src/System/Helperstext_helper.php

CodeIgniter

An open source application development framework for PHP

This content is released under the MIT License (MIT)

Copyright (c) 2014-2019 British Columbia Institute of Technology Copyright (c) 2019 CodeIgniter Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Functions

word_limiter()

word_limiter(string  $str, integer  $limit = 100, string  $end_char = '…') : string

Word Limiter

Limits a string to X number of words.

Parameters

string $str
integer $limit
string $end_char

the end character. Usually an ellipsis

Returns

string

character_limiter()

character_limiter(string  $str, integer  $n = 500, string  $end_char = '…') : string

Character Limiter

Limits the string based on the character count. Preserves complete words so the character count may not be exactly as specified.

Parameters

string $str
integer $n
string $end_char

the end character. Usually an ellipsis

Returns

string

ascii_to_entities()

ascii_to_entities(string  $str) : string

High ASCII to Entities

Converts high ASCII text and MS Word special characters to character entities

Parameters

string $str

Returns

string

entities_to_ascii()

entities_to_ascii(string  $str, boolean  $all = true) : string

Entities to ASCII

Converts character entities back to ASCII

Parameters

string $str
boolean $all

Returns

string

word_censor()

word_censor(string  $str, array  $censored, string  $replacement = '') : string

Word Censoring Function

Supply a string and an array of disallowed words and any matched words will be converted to #### or to the replacement word you've submitted.

Parameters

string $str

the text string

array $censored

the array of censored words

string $replacement

the optional replacement value

Returns

string

highlight_code()

highlight_code(string  $str) : string

Code Highlighter

Colorizes code strings

Parameters

string $str

the text string

Returns

string

highlight_phrase()

highlight_phrase(string  $str, string  $phrase, string  $tag_open = '<mark>', string  $tag_close = '</mark>') : string

Phrase Highlighter

Highlights a phrase within a text string

Parameters

string $str

the text string

string $phrase

the phrase you'd like to highlight

string $tag_open

the opening tag to precede the phrase with

string $tag_close

the closing tag to end the phrase with

Returns

string

convert_accented_characters()

convert_accented_characters(string  $str) : string

Convert Accented Foreign Characters to ASCII

Parameters

string $str

Input string

Returns

string

word_wrap()

word_wrap(string  $str, integer  $charlim = 76) : string

Word Wrap

Wraps text at the specified character. Maintains the integrity of words. Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor will URLs.

Parameters

string $str

the text string

integer $charlim

= 76 the number of characters to wrap at

Returns

string

ellipsize()

ellipsize(string  $str, integer  $max_length, mixed  $position = 1, string  $ellipsis = '&hellip;') : string

Ellipsize String

This function will strip tags from a string, split it at its max_length and ellipsize

Parameters

string $str

String to ellipsize

integer $max_length

Max length of string

mixed $position

int (1|0) or float, .5, .2, etc for position to split

string $ellipsis

ellipsis ; Default '...'

Returns

string —

Ellipsized string

strip_slashes()

strip_slashes(mixed  $str) : mixed

Strip Slashes

Removes slashes contained in a string or in an array

Parameters

mixed $str

string or array

Returns

mixed —

string or array

strip_quotes()

strip_quotes(string  $str) : string

Strip Quotes

Removes single and double quotes from a string

Parameters

string $str

Returns

string

quotes_to_entities()

quotes_to_entities(string  $str) : string

Quotes to Entities

Converts single and double quotes to entities

Parameters

string $str

Returns

string

reduce_multiples()

reduce_multiples(string  $str, string  $character = ',', boolean  $trim = false) : string

Reduce Multiples

Reduces multiple instances of a particular character. Example:

Fred, Bill,, Joe, Jimmy

becomes:

Fred, Bill, Joe, Jimmy

Parameters

string $str
string $character

the character you wish to reduce

boolean $trim

TRUE/FALSE - whether to trim the character from the beginning/end

Returns

string

random_string()

random_string(string  $type = 'alnum', integer  $len = 8) : string

Create a Random String

Useful for generating passwords or hashes.

Parameters

string $type

Type of random string. basic, alpha, alnum, numeric, nozero, md5, sha1, and crypto

integer $len

Number of characters

Returns

string

increment_string()

increment_string(string  $str, string  $separator = '_', integer  $first = 1) : string

Add's _1 to a string or increment the ending number to allow _2, _3, etc

Parameters

string $str

Required

string $separator

What should the duplicate number be appended with

integer $first

Which number should be used for the first dupe increment

Returns

string

alternator()

alternator() : string

Alternator

Allows strings to be alternated. See docs...

Returns

string

excerpt()

excerpt(string  $text, string  $phrase = null, integer  $radius = 100, string  $ellipsis = '...') : string

Excerpt.

Allows to extract a piece of text surrounding a word or phrase.

Parameters

string $text

String to search the phrase

string $phrase

Phrase that will be searched for.

integer $radius

The amount of characters returned around the phrase.

string $ellipsis

Ending that will be appended

Returns

string —

If no $phrase is passed, will generate an excerpt of $radius characters from the beginning of $text.