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