blob: ef47882fba4b54adffb952ab6dbd5ffc2e9bf58c [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001###########
2Text Helper
3###########
4
5The Text Helper file contains functions that assist in working with
6text.
7
Derek Jones2488ed22013-07-19 16:35:25 -07008.. contents::
9 :local:
10
11.. raw:: html
12
13 <div class="custom-index container"></div>
Derek Jones8ede1a22011-10-05 13:34:52 -050014
15Loading this Helper
16===================
17
Andrey Andreev442682e2012-11-08 22:52:12 +020018This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050019
20 $this->load->helper('text');
21
Derek Jones2488ed22013-07-19 16:35:25 -070022Available Functions
23===================
24
Derek Jones8ede1a22011-10-05 13:34:52 -050025The following functions are available:
26
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020027.. php:function:: word_limiter($str[, $limit = 100[, $end_char = '&#8230;']])
Andrey Andreev442682e2012-11-08 22:52:12 +020028
29 :param string $str: Input string
30 :param int $limit: Limit
31 :param string $end_char: End character (usually an ellipsis)
Andrey Andreev3de130c2014-02-07 23:31:49 +020032 :returns: Word-limited string
33 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +020034
Derek Jones2488ed22013-07-19 16:35:25 -070035 Truncates a string to the number of *words* specified. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050036
Derek Jones2488ed22013-07-19 16:35:25 -070037 $string = "Here is a nice text string consisting of eleven words.";
38 $string = word_limiter($string, 4);
Andrey Andreeve0da1532014-01-07 16:17:04 +020039 // Returns: Here is a nice
Derek Jones8ede1a22011-10-05 13:34:52 -050040
Derek Jones2488ed22013-07-19 16:35:25 -070041 The third parameter is an optional suffix added to the string. By
42 default it adds an ellipsis.
Derek Jones8ede1a22011-10-05 13:34:52 -050043
Derek Jones8ede1a22011-10-05 13:34:52 -050044
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020045.. php:function:: character_limiter($str[, $n = 500[, $end_char = '&#8230;']])
Derek Jones8ede1a22011-10-05 13:34:52 -050046
Andrey Andreev442682e2012-11-08 22:52:12 +020047 :param string $str: Input string
48 :param int $n: Number of characters
49 :param string $end_char: End character (usually an ellipsis)
Andrey Andreev3de130c2014-02-07 23:31:49 +020050 :returns: Character-limited string
51 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +020052
Derek Jones2488ed22013-07-19 16:35:25 -070053 Truncates a string to the number of *characters* specified. It
54 maintains the integrity of words so the character count may be slightly
Andrey Andreevba231aa2014-01-20 16:43:41 +020055 more or less than what you specify.
Andrey Andreev442682e2012-11-08 22:52:12 +020056
Derek Jones2488ed22013-07-19 16:35:25 -070057 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050058
Derek Jones2488ed22013-07-19 16:35:25 -070059 $string = "Here is a nice text string consisting of eleven words.";
60 $string = character_limiter($string, 20);
Andrey Andreeve0da1532014-01-07 16:17:04 +020061 // Returns: Here is a nice text string
Derek Jones8ede1a22011-10-05 13:34:52 -050062
Derek Jones2488ed22013-07-19 16:35:25 -070063 The third parameter is an optional suffix added to the string, if
64 undeclared this helper uses an ellipsis.
Derek Jones8ede1a22011-10-05 13:34:52 -050065
Derek Jones2488ed22013-07-19 16:35:25 -070066 .. note:: If you need to truncate to an exact number of characters please
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020067 see the :php:func:`ellipsize()` function below.
Eric Barnes399cca92011-12-14 11:04:14 -050068
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020069.. php:function:: ascii_to_entities($str)
Andrey Andreev442682e2012-11-08 22:52:12 +020070
71 :param string $str: Input string
Andrey Andreev3de130c2014-02-07 23:31:49 +020072 :returns: A string with ASCII values converted to entities
73 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +020074
Derek Jones2488ed22013-07-19 16:35:25 -070075 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 Jones8ede1a22011-10-05 13:34:52 -050082
Derek Jones2488ed22013-07-19 16:35:25 -070083 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050084
Derek Jones2488ed22013-07-19 16:35:25 -070085 $string = ascii_to_entities($string);
Derek Jones8ede1a22011-10-05 13:34:52 -050086
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020087.. php:function::entities_to_ascii($str[, $all = TRUE])
Andrey Andreev442682e2012-11-08 22:52:12 +020088
89 :param string $str: Input string
90 :param bool $all: Whether to convert unsafe entities as well
Andrey Andreev3de130c2014-02-07 23:31:49 +020091 :returns: A string with HTML entities converted to ASCII characters
92 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +020093
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020094 This function does the opposite of :php:func:`ascii_to_entities()`.
Derek Jones2488ed22013-07-19 16:35:25 -070095 It turns character entities back into ASCII.
Derek Jones8ede1a22011-10-05 13:34:52 -050096
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020097.. php:function:: convert_accented_characters($str)
Andrey Andreev442682e2012-11-08 22:52:12 +020098
99 :param string $str: Input string
Andrey Andreev3de130c2014-02-07 23:31:49 +0200100 :returns: A string with accented characters converted
101 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +0200102
Derek Jones2488ed22013-07-19 16:35:25 -0700103 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 Jones8ede1a22011-10-05 13:34:52 -0500106
Derek Jones2488ed22013-07-19 16:35:25 -0700107 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
Derek Jones2488ed22013-07-19 16:35:25 -0700109 $string = convert_accented_characters($string);
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
Derek Jones2488ed22013-07-19 16:35:25 -0700111 .. 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 Jones8ede1a22011-10-05 13:34:52 -0500114
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200115.. php:function:: word_censor($str, $censored[, $replacement = ''])
Andrey Andreev442682e2012-11-08 22:52:12 +0200116
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 Andreev3de130c2014-02-07 23:31:49 +0200120 :returns: Censored string
121 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +0200122
Derek Jones2488ed22013-07-19 16:35:25 -0700123 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 Jones8ede1a22011-10-05 13:34:52 -0500128
Derek Jones2488ed22013-07-19 16:35:25 -0700129 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
Derek Jones2488ed22013-07-19 16:35:25 -0700131 $disallowed = array('darn', 'shucks', 'golly', 'phooey');
132 $string = word_censor($string, $disallowed, 'Beep!');
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200134.. php:function:: highlight_code($str)
Andrey Andreev442682e2012-11-08 22:52:12 +0200135
136 :param string $str: Input string
Andrey Andreev3de130c2014-02-07 23:31:49 +0200137 :returns: String with code highlighted via HTML
138 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +0200139
Derek Jones2488ed22013-07-19 16:35:25 -0700140 Colorizes a string of code (PHP, HTML, etc.). Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
Derek Jones2488ed22013-07-19 16:35:25 -0700142 $string = highlight_code($string);
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
Derek Jones2488ed22013-07-19 16:35:25 -0700144 The function uses PHP's ``highlight_string()`` function, so the
145 colors used are the ones specified in your php.ini file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200148.. php:function:: highlight_phrase($str, $phrase[, $tag_open = '<mark>'[, $tag_close = '</mark>']])
Andrey Andreev442682e2012-11-08 22:52:12 +0200149
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 Andreev3de130c2014-02-07 23:31:49 +0200154 :returns: String with a phrase highlighted via HTML
155 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +0200156
Derek Jones2488ed22013-07-19 16:35:25 -0700157 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 Jones8ede1a22011-10-05 13:34:52 -0500161
Derek Jones2488ed22013-07-19 16:35:25 -0700162 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
Derek Jones2488ed22013-07-19 16:35:25 -0700164 $string = "Here is a nice text string about nothing in particular.";
165 echo highlight_phrase($string, "nice text", '<span style="color:#990000;">', '</span>');
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
Derek Jones2488ed22013-07-19 16:35:25 -0700167 The above code prints::
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
Derek Jones2488ed22013-07-19 16:35:25 -0700169 Here is a <span style="color:#990000;">nice text</span> string about nothing in particular.
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Andrey Andreeve0da1532014-01-07 16:17:04 +0200171 .. 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 Andreevcd3d9db2015-02-02 13:41:01 +0200181.. php:function:: word_wrap($str[, $charlim = 76])
Derek Jones8ede1a22011-10-05 13:34:52 -0500182
Andrey Andreev442682e2012-11-08 22:52:12 +0200183 :param string $str: Input string
184 :param int $charlim: Character limit
Andrey Andreev3de130c2014-02-07 23:31:49 +0200185 :returns: Word-wrapped string
186 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +0200187
Derek Jones2488ed22013-07-19 16:35:25 -0700188 Wraps text at the specified *character* count while maintaining
189 complete words.
Andrey Andreev442682e2012-11-08 22:52:12 +0200190
Derek Jones2488ed22013-07-19 16:35:25 -0700191 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500192
Derek Jones2488ed22013-07-19 16:35:25 -0700193 $string = "Here is a simple string of text that will help us demonstrate this function.";
194 echo word_wrap($string, 25);
Derek Jones8ede1a22011-10-05 13:34:52 -0500195
aneasystoned5326292015-07-26 16:47:25 +0800196 // Would produce:
197 // Here is a simple string
198 // of text that will help us
199 // demonstrate this
200 // function.
Derek Jones8ede1a22011-10-05 13:34:52 -0500201
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200202.. php:function:: ellipsize($str, $max_length[, $position = 1[, $ellipsis = '&hellip;']])
Andrey Andreev442682e2012-11-08 22:52:12 +0200203
204 :param string $str: Input string
205 :param int $max_length: String length limit
Andrey Andreev3de130c2014-02-07 23:31:49 +0200206 :param mixed $position: Position to split at (int or float)
Andrey Andreev442682e2012-11-08 22:52:12 +0200207 :param string $ellipsis: What to use as the ellipsis character
Andrey Andreev3de130c2014-02-07 23:31:49 +0200208 :returns: Ellipsized string
209 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +0200210
Derek Jones2488ed22013-07-19 16:35:25 -0700211 This function will strip tags from a string, split it at a defined
212 maximum length, and insert an ellipsis.
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
Derek Jones2488ed22013-07-19 16:35:25 -0700214 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 Jones8ede1a22011-10-05 13:34:52 -0500219
Derek Jones2488ed22013-07-19 16:35:25 -0700220 An optional forth parameter is the kind of ellipsis. By default,
221 &hellip; will be inserted.
Derek Jones8ede1a22011-10-05 13:34:52 -0500222
Derek Jones2488ed22013-07-19 16:35:25 -0700223 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500224
Derek Jones2488ed22013-07-19 16:35:25 -0700225 $str = 'this_string_is_entirely_too_long_and_might_break_my_design.jpg';
226 echo ellipsize($str, 32, .5);
Derek Jones8ede1a22011-10-05 13:34:52 -0500227
Derek Jones2488ed22013-07-19 16:35:25 -0700228 Produces::
Derek Jones8ede1a22011-10-05 13:34:52 -0500229
Derek Jones2488ed22013-07-19 16:35:25 -0700230 this_string_is_e&hellip;ak_my_design.jpg