blob: e9764327512f85af14cf9a9de6439e807ac8513d [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
13This helper is loaded using the following code
14
15::
16
17 $this->load->helper('text');
18
19The following functions are available:
20
21word_limiter()
22==============
23
24Truncates a string to the number of **words** specified. Example::
25
26 $string = "Here is a nice text string consisting of eleven words.";
27 $string = word_limiter($string, 4);
28 // Returns: Here is a nice…
29
30The third parameter is an optional suffix added to the string. By
31default it adds an ellipsis.
32
33character_limiter()
34===================
35
36Truncates a string to the number of **characters** specified. It
37maintains the integrity of words so the character count may be slightly
38more or less then what you specify. Example
39
40::
41
42 $string = "Here is a nice text string consisting of eleven words.";
43 $string = character_limiter($string, 20);
44 // Returns: Here is a nice text string…
45
46The third parameter is an optional suffix added to the string, if
47undeclared this helper uses an ellipsis.
48
49ascii_to_entities()
50===================
51
52Converts ASCII values to character entities, including high ASCII and MS
53Word characters that can cause problems when used in a web page, so that
54they can be shown consistently regardless of browser settings or stored
55reliably in a database. There is some dependence on your server's
56supported character sets, so it may not be 100% reliable in all cases,
57but for the most part it should correctly identify characters outside
58the normal range (like accented characters). Example
59
60::
61
62 $string = ascii_to_entities($string);
63
64entities_to_ascii()
65===================
66
67This function does the opposite of the previous one; it turns character
68entities back into ASCII.
69
70convert_accented_characters()
71=============================
72
73Transliterates high ASCII characters to low ASCII equivalents, useful
74when non-English characters need to be used where only standard ASCII
75characters are safely used, for instance, in URLs.
76
77::
78
79 $string = convert_accented_characters($string);
80
81This function uses a companion config file
82`application/config/foreign_chars.php` to define the to and from array
83for transliteration.
84
85word_censor()
86=============
87
88Enables you to censor words within a text string. The first parameter
89will contain the original string. The second will contain an array of
90words which you disallow. The third (optional) parameter can contain a
91replacement value for the words. If not specified they are replaced with
92pound signs: ####. Example
93
94::
95
96 $disallowed = array('darn', 'shucks', 'golly', 'phooey');
97 $string = word_censor($string, $disallowed, 'Beep!');
98
99highlight_code()
100================
101
102Colorizes a string of code (PHP, HTML, etc.). Example::
103
104 $string = highlight_code($string);
105
106The function uses PHP's highlight_string() function, so the colors used
107are the ones specified in your php.ini file.
108
109highlight_phrase()
110==================
111
112Will highlight a phrase within a text string. The first parameter will
113contain the original string, the second will contain the phrase you wish
114to highlight. The third and fourth parameters will contain the
115opening/closing HTML tags you would like the phrase wrapped in. Example
116
117::
118
119 $string = "Here is a nice text string about nothing in particular.";
120 $string = highlight_phrase($string, "nice text", '<span style="color:#990000">', '</span>');
121
122The above text returns:
123
124Here is a nice text string about nothing in particular.
125
126word_wrap()
127===========
128
129Wraps text at the specified **character** count while maintaining
130complete words. Example
131
132::
133
134 $string = "Here is a simple string of text that will help us demonstrate this function.";
135 echo word_wrap($string, 25);
136
137 // Would produce: Here is a simple string of text that will help us demonstrate this function
138
139ellipsize()
140===========
141
142This function will strip tags from a string, split it at a defined
143maximum length, and insert an ellipsis.
144
145The first parameter is the string to ellipsize, the second is the number
146of characters in the final string. The third parameter is where in the
147string the ellipsis should appear from 0 - 1, left to right. For
148example. a value of 1 will place the ellipsis at the right of the
149string, .5 in the middle, and 0 at the left.
150
151An optional forth parameter is the kind of ellipsis. By default,
152&hellip; will be inserted.
153
154::
155
156 $str = 'this_string_is_entirely_too_long_and_might_break_my_design.jpg';
157 echo ellipsize($str, 32, .5);
158
159Produces:
160
161::
162
163 this_string_is_e&hellip;ak_my_design.jpg
164