blob: 8cb2d6f96a8516b52b837cfba6dd6d8151039e8a [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
Eric Barnes399cca92011-12-14 11:04:14 -050049**Note:** If you need to truncate to an exact number of characters please see
50the :ref:`ellipsize` function below.
51
Derek Jones8ede1a22011-10-05 13:34:52 -050052ascii_to_entities()
53===================
54
55Converts ASCII values to character entities, including high ASCII and MS
56Word characters that can cause problems when used in a web page, so that
57they can be shown consistently regardless of browser settings or stored
58reliably in a database. There is some dependence on your server's
59supported character sets, so it may not be 100% reliable in all cases,
60but for the most part it should correctly identify characters outside
61the normal range (like accented characters). Example
62
63::
64
65 $string = ascii_to_entities($string);
66
67entities_to_ascii()
68===================
69
70This function does the opposite of the previous one; it turns character
71entities back into ASCII.
72
73convert_accented_characters()
74=============================
75
76Transliterates high ASCII characters to low ASCII equivalents, useful
77when non-English characters need to be used where only standard ASCII
78characters are safely used, for instance, in URLs.
79
80::
81
82 $string = convert_accented_characters($string);
83
84This function uses a companion config file
85`application/config/foreign_chars.php` to define the to and from array
86for transliteration.
87
88word_censor()
89=============
90
91Enables you to censor words within a text string. The first parameter
92will contain the original string. The second will contain an array of
93words which you disallow. The third (optional) parameter can contain a
94replacement value for the words. If not specified they are replaced with
95pound signs: ####. Example
96
97::
98
99 $disallowed = array('darn', 'shucks', 'golly', 'phooey');
100 $string = word_censor($string, $disallowed, 'Beep!');
101
102highlight_code()
103================
104
105Colorizes a string of code (PHP, HTML, etc.). Example::
106
107 $string = highlight_code($string);
108
109The function uses PHP's highlight_string() function, so the colors used
110are the ones specified in your php.ini file.
111
112highlight_phrase()
113==================
114
115Will highlight a phrase within a text string. The first parameter will
116contain the original string, the second will contain the phrase you wish
117to highlight. The third and fourth parameters will contain the
118opening/closing HTML tags you would like the phrase wrapped in. Example
119
120::
121
122 $string = "Here is a nice text string about nothing in particular.";
123 $string = highlight_phrase($string, "nice text", '<span style="color:#990000">', '</span>');
124
125The above text returns:
126
127Here is a nice text string about nothing in particular.
128
129word_wrap()
130===========
131
132Wraps text at the specified **character** count while maintaining
133complete words. Example
134
135::
136
137 $string = "Here is a simple string of text that will help us demonstrate this function.";
138 echo word_wrap($string, 25);
139
140 // Would produce: Here is a simple string of text that will help us demonstrate this function
141
Eric Barnes399cca92011-12-14 11:04:14 -0500142.. _ellipsize:
143
Derek Jones8ede1a22011-10-05 13:34:52 -0500144ellipsize()
145===========
146
147This function will strip tags from a string, split it at a defined
148maximum length, and insert an ellipsis.
149
150The first parameter is the string to ellipsize, the second is the number
151of characters in the final string. The third parameter is where in the
152string the ellipsis should appear from 0 - 1, left to right. For
153example. a value of 1 will place the ellipsis at the right of the
154string, .5 in the middle, and 0 at the left.
155
156An optional forth parameter is the kind of ellipsis. By default,
157&hellip; will be inserted.
158
159::
160
161 $str = 'this_string_is_entirely_too_long_and_might_break_my_design.jpg';
162 echo ellipsize($str, 32, .5);
163
164Produces:
165
166::
167
168 this_string_is_e&hellip;ak_my_design.jpg
169