blob: 2b71432efad1104d6b704f42d2a761961fa7eef2 [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
8.. contents:: Page Contents
9
10Loading this Helper
11===================
12
Andrey Andreev442682e2012-11-08 22:52:12 +020013This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050014
15 $this->load->helper('text');
16
17The following functions are available:
18
19word_limiter()
20==============
21
Andrey Andreev442682e2012-11-08 22:52:12 +020022.. 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
29Truncates a string to the number of *words* specified. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050030
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
35The third parameter is an optional suffix added to the string. By
36default it adds an ellipsis.
37
38character_limiter()
39===================
40
Andrey Andreev442682e2012-11-08 22:52:12 +020041.. php:function:: character_limiter($str, $n = 500, $end_char = '…')
Derek Jones8ede1a22011-10-05 13:34:52 -050042
Andrey Andreev442682e2012-11-08 22:52:12 +020043 :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
48Truncates a string to the number of *characters* specified. It
49maintains the integrity of words so the character count may be slightly
50more or less then what you specify.
51
52Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050053
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
58The third parameter is an optional suffix added to the string, if
59undeclared this helper uses an ellipsis.
60
Andrey Andreev442682e2012-11-08 22:52:12 +020061.. note:: If you need to truncate to an exact number of characters please
62 see the :php:func:`ellipsize()` function below.
Eric Barnes399cca92011-12-14 11:04:14 -050063
Derek Jones8ede1a22011-10-05 13:34:52 -050064ascii_to_entities()
65===================
66
Andrey Andreev442682e2012-11-08 22:52:12 +020067.. php:function:: ascii_to_entities($str)
68
69 :param string $str: Input string
70 :returns: string
71
Derek Jones8ede1a22011-10-05 13:34:52 -050072Converts ASCII values to character entities, including high ASCII and MS
73Word characters that can cause problems when used in a web page, so that
74they can be shown consistently regardless of browser settings or stored
75reliably in a database. There is some dependence on your server's
76supported character sets, so it may not be 100% reliable in all cases,
77but for the most part it should correctly identify characters outside
Andrey Andreev442682e2012-11-08 22:52:12 +020078the normal range (like accented characters).
Derek Jones8ede1a22011-10-05 13:34:52 -050079
Andrey Andreev442682e2012-11-08 22:52:12 +020080Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050081
82 $string = ascii_to_entities($string);
83
84entities_to_ascii()
85===================
86
Andrey Andreev442682e2012-11-08 22:52:12 +020087.. 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
93This function does the opposite of :php:func:`ascii_to_entities()`.
94It turns character entities back into ASCII.
Derek Jones8ede1a22011-10-05 13:34:52 -050095
96convert_accented_characters()
97=============================
98
Andrey Andreev442682e2012-11-08 22:52:12 +020099.. php:function:: convert_accented_characters($str)
100
101 :param string $str: Input string
102 :returns: string
103
104Transliterates high ASCII characters to low ASCII equivalents. Useful
Derek Jones8ede1a22011-10-05 13:34:52 -0500105when non-English characters need to be used where only standard ASCII
106characters are safely used, for instance, in URLs.
107
Andrey Andreev442682e2012-11-08 22:52:12 +0200108Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500109
110 $string = convert_accented_characters($string);
111
Andrey Andreev442682e2012-11-08 22:52:12 +0200112.. 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 Jones8ede1a22011-10-05 13:34:52 -0500115
116word_censor()
117=============
118
Andrey Andreev442682e2012-11-08 22:52:12 +0200119.. 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 Jones8ede1a22011-10-05 13:34:52 -0500126Enables you to censor words within a text string. The first parameter
127will contain the original string. The second will contain an array of
Andrey Andreev442682e2012-11-08 22:52:12 +0200128words which you disallow. The third (optional) parameter can contain
129a replacement value for the words. If not specified they are replaced
130with pound signs: ####.
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Andrey Andreev442682e2012-11-08 22:52:12 +0200132Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
134 $disallowed = array('darn', 'shucks', 'golly', 'phooey');
135 $string = word_censor($string, $disallowed, 'Beep!');
136
137highlight_code()
138================
139
Andrey Andreev442682e2012-11-08 22:52:12 +0200140.. php:function:: highlight_code($str)
141
142 :param string $str: Input string
143 :returns: string
144
Derek Jones8ede1a22011-10-05 13:34:52 -0500145Colorizes a string of code (PHP, HTML, etc.). Example::
146
147 $string = highlight_code($string);
148
Andrey Andreev442682e2012-11-08 22:52:12 +0200149The function uses PHP's ``highlight_string()`` function, so the
150colors used are the ones specified in your php.ini file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500151
152highlight_phrase()
153==================
154
Andrey Andreev442682e2012-11-08 22:52:12 +0200155.. 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 Jones8ede1a22011-10-05 13:34:52 -0500163Will highlight a phrase within a text string. The first parameter will
164contain the original string, the second will contain the phrase you wish
165to highlight. The third and fourth parameters will contain the
Andrey Andreev442682e2012-11-08 22:52:12 +0200166opening/closing HTML tags you would like the phrase wrapped in.
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
Andrey Andreev442682e2012-11-08 22:52:12 +0200168Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500169
170 $string = "Here is a nice text string about nothing in particular.";
Andrey Andreev442682e2012-11-08 22:52:12 +0200171 echo highlight_phrase($string, "nice text", '<span style="color:#990000;">', '</span>');
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
Andrey Andreev442682e2012-11-08 22:52:12 +0200173The above code prints::
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
Andrey Andreev442682e2012-11-08 22:52:12 +0200175 Here is a <span style="color:#990000;">nice text</span> string about nothing in particular.
Derek Jones8ede1a22011-10-05 13:34:52 -0500176
177word_wrap()
178===========
179
Andrey Andreev442682e2012-11-08 22:52:12 +0200180.. php:function:: word_wrap($str, $charlim = 76)
Derek Jones8ede1a22011-10-05 13:34:52 -0500181
Andrey Andreev442682e2012-11-08 22:52:12 +0200182 :param string $str: Input string
183 :param int $charlim: Character limit
184 :returns: string
185
186Wraps text at the specified *character* count while maintaining
187complete words.
188
189Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500190
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
Derek Jones8ede1a22011-10-05 13:34:52 -0500196ellipsize()
197===========
198
Andrey Andreev442682e2012-11-08 22:52:12 +0200199.. php:function:: ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
200
201 :param string $str: Input string
202 :param int $max_length: String length limit
203 :param mixed $position: Position to split at
204 (int or float)
205 :param string $ellipsis: What to use as the ellipsis character
206 :returns: string
207
Derek Jones8ede1a22011-10-05 13:34:52 -0500208This function will strip tags from a string, split it at a defined
209maximum length, and insert an ellipsis.
210
211The first parameter is the string to ellipsize, the second is the number
212of characters in the final string. The third parameter is where in the
213string the ellipsis should appear from 0 - 1, left to right. For
214example. a value of 1 will place the ellipsis at the right of the
215string, .5 in the middle, and 0 at the left.
216
217An optional forth parameter is the kind of ellipsis. By default,
218&hellip; will be inserted.
219
Andrey Andreev442682e2012-11-08 22:52:12 +0200220Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500221
222 $str = 'this_string_is_entirely_too_long_and_might_break_my_design.jpg';
223 echo ellipsize($str, 32, .5);
224
Andrey Andreev442682e2012-11-08 22:52:12 +0200225Produces::
Derek Jones8ede1a22011-10-05 13:34:52 -0500226
Andrey Andreev442682e2012-11-08 22:52:12 +0200227 this_string_is_e&hellip;ak_my_design.jpg