blob: 0cacd1b6926459308e3bbf0453e70f90a6d5ee07 [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
Derek Jones2488ed22013-07-19 16:35:25 -070027.. 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)
32 :returns: string
33
Derek Jones2488ed22013-07-19 16:35:25 -070034 Truncates a string to the number of *words* specified. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050035
Derek Jones2488ed22013-07-19 16:35:25 -070036 $string = "Here is a nice text string consisting of eleven words.";
37 $string = word_limiter($string, 4);
38 // Returns: Here is a nice…
Derek Jones8ede1a22011-10-05 13:34:52 -050039
Derek Jones2488ed22013-07-19 16:35:25 -070040 The third parameter is an optional suffix added to the string. By
41 default it adds an ellipsis.
Derek Jones8ede1a22011-10-05 13:34:52 -050042
Derek Jones8ede1a22011-10-05 13:34:52 -050043
Derek Jones2488ed22013-07-19 16:35:25 -070044.. function:: character_limiter($str[, $n = 500[, $end_char = '&#8230;']])
Derek Jones8ede1a22011-10-05 13:34:52 -050045
Andrey Andreev442682e2012-11-08 22:52:12 +020046 :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 Jones2488ed22013-07-19 16:35:25 -070051 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 Andreev442682e2012-11-08 22:52:12 +020054
Derek Jones2488ed22013-07-19 16:35:25 -070055 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050056
Derek Jones2488ed22013-07-19 16:35:25 -070057 $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 Jones8ede1a22011-10-05 13:34:52 -050060
Derek Jones2488ed22013-07-19 16:35:25 -070061 The third parameter is an optional suffix added to the string, if
62 undeclared this helper uses an ellipsis.
Derek Jones8ede1a22011-10-05 13:34:52 -050063
Derek Jones2488ed22013-07-19 16:35:25 -070064 .. note:: If you need to truncate to an exact number of characters please
65 see the :func:`ellipsize()` function below.
Eric Barnes399cca92011-12-14 11:04:14 -050066
Derek Jones8ede1a22011-10-05 13:34:52 -050067
Derek Jonesb8c283a2013-07-19 16:02:53 -070068.. function:: ascii_to_entities($str)
Andrey Andreev442682e2012-11-08 22:52:12 +020069
70 :param string $str: Input string
71 :returns: string
72
Derek Jones2488ed22013-07-19 16:35:25 -070073 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 Jones8ede1a22011-10-05 13:34:52 -050080
Derek Jones2488ed22013-07-19 16:35:25 -070081 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050082
Derek Jones2488ed22013-07-19 16:35:25 -070083 $string = ascii_to_entities($string);
Derek Jones8ede1a22011-10-05 13:34:52 -050084
Derek Jones8ede1a22011-10-05 13:34:52 -050085
Derek Jones2488ed22013-07-19 16:35:25 -070086.. function::entities_to_ascii($str[, $all = TRUE])
Andrey Andreev442682e2012-11-08 22:52:12 +020087
88 :param string $str: Input string
89 :param bool $all: Whether to convert unsafe entities as well
90 :returns: string
91
Derek Jones2488ed22013-07-19 16:35:25 -070092 This function does the opposite of :func:`ascii_to_entities()`.
93 It turns character entities back into ASCII.
Derek Jones8ede1a22011-10-05 13:34:52 -050094
Derek Jones8ede1a22011-10-05 13:34:52 -050095
Derek Jonesb8c283a2013-07-19 16:02:53 -070096.. function:: convert_accented_characters($str)
Andrey Andreev442682e2012-11-08 22:52:12 +020097
98 :param string $str: Input string
99 :returns: string
100
Derek Jones2488ed22013-07-19 16:35:25 -0700101 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 Jones8ede1a22011-10-05 13:34:52 -0500104
Derek Jones2488ed22013-07-19 16:35:25 -0700105 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500106
Derek Jones2488ed22013-07-19 16:35:25 -0700107 $string = convert_accented_characters($string);
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
Derek Jones2488ed22013-07-19 16:35:25 -0700109 .. 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 Jones8ede1a22011-10-05 13:34:52 -0500112
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Derek Jones2488ed22013-07-19 16:35:25 -0700114.. function:: word_censor($str, $censored[, $replacement = ''])
Andrey Andreev442682e2012-11-08 22:52:12 +0200115
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 Jones2488ed22013-07-19 16:35:25 -0700121 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 Jones8ede1a22011-10-05 13:34:52 -0500126
Derek Jones2488ed22013-07-19 16:35:25 -0700127 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
Derek Jones2488ed22013-07-19 16:35:25 -0700129 $disallowed = array('darn', 'shucks', 'golly', 'phooey');
130 $string = word_censor($string, $disallowed, 'Beep!');
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Derek Jones8ede1a22011-10-05 13:34:52 -0500132
Derek Jonesb8c283a2013-07-19 16:02:53 -0700133.. function:: highlight_code($str)
Andrey Andreev442682e2012-11-08 22:52:12 +0200134
135 :param string $str: Input string
136 :returns: string
137
Derek Jones2488ed22013-07-19 16:35:25 -0700138 Colorizes a string of code (PHP, HTML, etc.). Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500139
Derek Jones2488ed22013-07-19 16:35:25 -0700140 $string = highlight_code($string);
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
Derek Jones2488ed22013-07-19 16:35:25 -0700142 The function uses PHP's ``highlight_string()`` function, so the
143 colors used are the ones specified in your php.ini file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
Derek Jones8ede1a22011-10-05 13:34:52 -0500145
Derek Jones2488ed22013-07-19 16:35:25 -0700146.. function:: highlight_phrase($str, $phrase[, $tag_open = '<strong>'[, $tag_close = '</strong>']])
Andrey Andreev442682e2012-11-08 22:52:12 +0200147
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 Jones2488ed22013-07-19 16:35:25 -0700154 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 Jones8ede1a22011-10-05 13:34:52 -0500158
Derek Jones2488ed22013-07-19 16:35:25 -0700159 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500160
Derek Jones2488ed22013-07-19 16:35:25 -0700161 $string = "Here is a nice text string about nothing in particular.";
162 echo highlight_phrase($string, "nice text", '<span style="color:#990000;">', '</span>');
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
Derek Jones2488ed22013-07-19 16:35:25 -0700164 The above code prints::
Derek Jones8ede1a22011-10-05 13:34:52 -0500165
Derek Jones2488ed22013-07-19 16:35:25 -0700166 Here is a <span style="color:#990000;">nice text</span> string about nothing in particular.
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
Derek Jones2488ed22013-07-19 16:35:25 -0700169.. function:: word_wrap($str[, $charlim = 76])
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Andrey Andreev442682e2012-11-08 22:52:12 +0200171 :param string $str: Input string
172 :param int $charlim: Character limit
173 :returns: string
174
Derek Jones2488ed22013-07-19 16:35:25 -0700175 Wraps text at the specified *character* count while maintaining
176 complete words.
Andrey Andreev442682e2012-11-08 22:52:12 +0200177
Derek Jones2488ed22013-07-19 16:35:25 -0700178 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
Derek Jones2488ed22013-07-19 16:35:25 -0700180 $string = "Here is a simple string of text that will help us demonstrate this function.";
181 echo word_wrap($string, 25);
Derek Jones8ede1a22011-10-05 13:34:52 -0500182
Derek Jones2488ed22013-07-19 16:35:25 -0700183 // Would produce: Here is a simple string of text that will help us demonstrate this function
Derek Jones8ede1a22011-10-05 13:34:52 -0500184
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200185
Derek Jones2488ed22013-07-19 16:35:25 -0700186.. function:: ellipsize($str, $max_length[, $position = 1[, $ellipsis = '&hellip;']])
Andrey Andreev442682e2012-11-08 22:52:12 +0200187
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 Jones2488ed22013-07-19 16:35:25 -0700195 This function will strip tags from a string, split it at a defined
196 maximum length, and insert an ellipsis.
Derek Jones8ede1a22011-10-05 13:34:52 -0500197
Derek Jones2488ed22013-07-19 16:35:25 -0700198 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 Jones8ede1a22011-10-05 13:34:52 -0500203
Derek Jones2488ed22013-07-19 16:35:25 -0700204 An optional forth parameter is the kind of ellipsis. By default,
205 &hellip; will be inserted.
Derek Jones8ede1a22011-10-05 13:34:52 -0500206
Derek Jones2488ed22013-07-19 16:35:25 -0700207 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500208
Derek Jones2488ed22013-07-19 16:35:25 -0700209 $str = 'this_string_is_entirely_too_long_and_might_break_my_design.jpg';
210 echo ellipsize($str, 32, .5);
Derek Jones8ede1a22011-10-05 13:34:52 -0500211
Derek Jones2488ed22013-07-19 16:35:25 -0700212 Produces::
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
Derek Jones2488ed22013-07-19 16:35:25 -0700214 this_string_is_e&hellip;ak_my_design.jpg