Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ########### |
| 2 | Text Helper |
| 3 | ########### |
| 4 | |
| 5 | The Text Helper file contains functions that assist in working with |
| 6 | text. |
| 7 | |
| 8 | .. contents:: Page Contents |
| 9 | |
| 10 | Loading this Helper |
| 11 | =================== |
| 12 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 13 | This helper is loaded using the following code:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 14 | |
| 15 | $this->load->helper('text'); |
| 16 | |
| 17 | The following functions are available: |
| 18 | |
| 19 | word_limiter() |
| 20 | ============== |
| 21 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 22 | .. php:function:: word_limiter($str, $limit = 100, $end_char = '…') |
| 23 | |
| 24 | :param string $str: Input string |
| 25 | :param int $limit: Limit |
| 26 | :param string $end_char: End character (usually an ellipsis) |
| 27 | :returns: string |
| 28 | |
| 29 | Truncates a string to the number of *words* specified. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | |
| 31 | $string = "Here is a nice text string consisting of eleven words."; |
| 32 | $string = word_limiter($string, 4); |
| 33 | // Returns: Here is a nice… |
| 34 | |
| 35 | The third parameter is an optional suffix added to the string. By |
| 36 | default it adds an ellipsis. |
| 37 | |
| 38 | character_limiter() |
| 39 | =================== |
| 40 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 41 | .. php:function:: character_limiter($str, $n = 500, $end_char = '…') |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 42 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 43 | :param string $str: Input string |
| 44 | :param int $n: Number of characters |
| 45 | :param string $end_char: End character (usually an ellipsis) |
| 46 | :returns: string |
| 47 | |
| 48 | Truncates a string to the number of *characters* specified. It |
| 49 | maintains the integrity of words so the character count may be slightly |
| 50 | more or less then what you specify. |
| 51 | |
| 52 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | |
| 54 | $string = "Here is a nice text string consisting of eleven words."; |
| 55 | $string = character_limiter($string, 20); |
| 56 | // Returns: Here is a nice text string… |
| 57 | |
| 58 | The third parameter is an optional suffix added to the string, if |
| 59 | undeclared this helper uses an ellipsis. |
| 60 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 61 | .. note:: If you need to truncate to an exact number of characters please |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 62 | see the :ref:`ellipsize()` function below. |
Eric Barnes | 399cca9 | 2011-12-14 11:04:14 -0500 | [diff] [blame] | 63 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 64 | ascii_to_entities() |
| 65 | =================== |
| 66 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 67 | .. php:function:: ascii_to_entities($str) |
| 68 | |
| 69 | :param string $str: Input string |
| 70 | :returns: string |
| 71 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 72 | Converts ASCII values to character entities, including high ASCII and MS |
| 73 | Word characters that can cause problems when used in a web page, so that |
| 74 | they can be shown consistently regardless of browser settings or stored |
| 75 | reliably in a database. There is some dependence on your server's |
| 76 | supported character sets, so it may not be 100% reliable in all cases, |
| 77 | but for the most part it should correctly identify characters outside |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 78 | the normal range (like accented characters). |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 79 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 80 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 81 | |
| 82 | $string = ascii_to_entities($string); |
| 83 | |
| 84 | entities_to_ascii() |
| 85 | =================== |
| 86 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 87 | .. php:function::entities_to_ascii($str, $all = TRUE) |
| 88 | |
| 89 | :param string $str: Input string |
| 90 | :param bool $all: Whether to convert unsafe entities as well |
| 91 | :returns: string |
| 92 | |
| 93 | This function does the opposite of :php:func:`ascii_to_entities()`. |
| 94 | It turns character entities back into ASCII. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 95 | |
| 96 | convert_accented_characters() |
| 97 | ============================= |
| 98 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 99 | .. php:function:: convert_accented_characters($str) |
| 100 | |
| 101 | :param string $str: Input string |
| 102 | :returns: string |
| 103 | |
| 104 | Transliterates high ASCII characters to low ASCII equivalents. Useful |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 105 | when non-English characters need to be used where only standard ASCII |
| 106 | characters are safely used, for instance, in URLs. |
| 107 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 108 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 109 | |
| 110 | $string = convert_accented_characters($string); |
| 111 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 112 | .. note:: This function uses a companion config file |
| 113 | `application/config/foreign_chars.php` to define the to and |
| 114 | from array for transliteration. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 115 | |
| 116 | word_censor() |
| 117 | ============= |
| 118 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 119 | .. php:function:: word_censor($str, $censored, $replacement = '') |
| 120 | |
| 121 | :param string $str: Input string |
| 122 | :param array $censored: List of bad words to censor |
| 123 | :param string $replacement: What to replace bad words with |
| 124 | :returns: string |
| 125 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 126 | Enables you to censor words within a text string. The first parameter |
| 127 | will contain the original string. The second will contain an array of |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 128 | words which you disallow. The third (optional) parameter can contain |
| 129 | a replacement value for the words. If not specified they are replaced |
| 130 | with pound signs: ####. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 132 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 133 | |
| 134 | $disallowed = array('darn', 'shucks', 'golly', 'phooey'); |
| 135 | $string = word_censor($string, $disallowed, 'Beep!'); |
| 136 | |
| 137 | highlight_code() |
| 138 | ================ |
| 139 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 140 | .. php:function:: highlight_code($str) |
| 141 | |
| 142 | :param string $str: Input string |
| 143 | :returns: string |
| 144 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 145 | Colorizes a string of code (PHP, HTML, etc.). Example:: |
| 146 | |
| 147 | $string = highlight_code($string); |
| 148 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 149 | The function uses PHP's ``highlight_string()`` function, so the |
| 150 | colors used are the ones specified in your php.ini file. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 151 | |
| 152 | highlight_phrase() |
| 153 | ================== |
| 154 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 155 | .. php:function:: highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>') |
| 156 | |
| 157 | :param string $str: Input string |
| 158 | :param string $phrase: Phrase to highlight |
| 159 | :param string $tag_open: Opening tag used for the highlight |
| 160 | :param string $tag_close: Closing tag for the highlight |
| 161 | :returns: string |
| 162 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 163 | Will highlight a phrase within a text string. The first parameter will |
| 164 | contain the original string, the second will contain the phrase you wish |
| 165 | to highlight. The third and fourth parameters will contain the |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 166 | opening/closing HTML tags you would like the phrase wrapped in. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 167 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 168 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 169 | |
| 170 | $string = "Here is a nice text string about nothing in particular."; |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 171 | echo highlight_phrase($string, "nice text", '<span style="color:#990000;">', '</span>'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 172 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 173 | The above code prints:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 174 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 175 | Here is a <span style="color:#990000;">nice text</span> string about nothing in particular. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 176 | |
| 177 | word_wrap() |
| 178 | =========== |
| 179 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 180 | .. php:function:: word_wrap($str, $charlim = 76) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 181 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 182 | :param string $str: Input string |
| 183 | :param int $charlim: Character limit |
| 184 | :returns: string |
| 185 | |
| 186 | Wraps text at the specified *character* count while maintaining |
| 187 | complete words. |
| 188 | |
| 189 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 190 | |
| 191 | $string = "Here is a simple string of text that will help us demonstrate this function."; |
| 192 | echo word_wrap($string, 25); |
| 193 | |
| 194 | // Would produce: Here is a simple string of text that will help us demonstrate this function |
| 195 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 196 | .. _ellipsize(): |
| 197 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 198 | ellipsize() |
| 199 | =========== |
| 200 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 201 | .. php:function:: ellipsize($str, $max_length, $position = 1, $ellipsis = '…') |
| 202 | |
| 203 | :param string $str: Input string |
| 204 | :param int $max_length: String length limit |
| 205 | :param mixed $position: Position to split at |
| 206 | (int or float) |
| 207 | :param string $ellipsis: What to use as the ellipsis character |
| 208 | :returns: string |
| 209 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 210 | This function will strip tags from a string, split it at a defined |
| 211 | maximum length, and insert an ellipsis. |
| 212 | |
| 213 | The first parameter is the string to ellipsize, the second is the number |
| 214 | of characters in the final string. The third parameter is where in the |
| 215 | string the ellipsis should appear from 0 - 1, left to right. For |
| 216 | example. a value of 1 will place the ellipsis at the right of the |
| 217 | string, .5 in the middle, and 0 at the left. |
| 218 | |
| 219 | An optional forth parameter is the kind of ellipsis. By default, |
| 220 | … will be inserted. |
| 221 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 222 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 223 | |
| 224 | $str = 'this_string_is_entirely_too_long_and_might_break_my_design.jpg'; |
| 225 | echo ellipsize($str, 32, .5); |
| 226 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 227 | Produces:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 228 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 229 | this_string_is_e…ak_my_design.jpg |