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