Take two:
[www-register-wizard.git] / helpers / text_helper.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * CodeIgniter\r
4  *\r
5  * An open source application development framework for PHP 4.3.2 or newer\r
6  *\r
7  * @package             CodeIgniter\r
8  * @author              ExpressionEngine Dev Team\r
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.\r
10  * @license             http://codeigniter.com/user_guide/license.html\r
11  * @link                http://codeigniter.com\r
12  * @since               Version 1.0\r
13  * @filesource\r
14  */\r
15 \r
16 // ------------------------------------------------------------------------\r
17 \r
18 /**\r
19  * CodeIgniter Text Helpers\r
20  *\r
21  * @package             CodeIgniter\r
22  * @subpackage  Helpers\r
23  * @category    Helpers\r
24  * @author              ExpressionEngine Dev Team\r
25  * @link                http://codeigniter.com/user_guide/helpers/text_helper.html\r
26  */\r
27 \r
28 // ------------------------------------------------------------------------\r
29 \r
30 /**\r
31  * Word Limiter\r
32  *\r
33  * Limits a string to X number of words.\r
34  *\r
35  * @access      public\r
36  * @param       string\r
37  * @param       integer\r
38  * @param       string  the end character. Usually an ellipsis\r
39  * @return      string\r
40  */     \r
41 if ( ! function_exists('word_limiter'))\r
42 {\r
43         function word_limiter($str, $limit = 100, $end_char = '&#8230;')\r
44         {\r
45                 if (trim($str) == '')\r
46                 {\r
47                         return $str;\r
48                 }\r
49         \r
50                 preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);\r
51                         \r
52                 if (strlen($str) == strlen($matches[0]))\r
53                 {\r
54                         $end_char = '';\r
55                 }\r
56                 \r
57                 return rtrim($matches[0]).$end_char;\r
58         }\r
59 }\r
60         \r
61 // ------------------------------------------------------------------------\r
62 \r
63 /**\r
64  * Character Limiter\r
65  *\r
66  * Limits the string based on the character count.  Preserves complete words\r
67  * so the character count may not be exactly as specified.\r
68  *\r
69  * @access      public\r
70  * @param       string\r
71  * @param       integer\r
72  * @param       string  the end character. Usually an ellipsis\r
73  * @return      string\r
74  */     \r
75 if ( ! function_exists('character_limiter'))\r
76 {\r
77         function character_limiter($str, $n = 500, $end_char = '&#8230;')\r
78         {\r
79                 if (strlen($str) < $n)\r
80                 {\r
81                         return $str;\r
82                 }\r
83                 \r
84                 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));\r
85 \r
86                 if (strlen($str) <= $n)\r
87                 {\r
88                         return $str;\r
89                 }\r
90                                                                         \r
91                 $out = "";\r
92                 foreach (explode(' ', trim($str)) as $val)\r
93                 {\r
94                         $out .= $val.' ';                       \r
95                         if (strlen($out) >= $n)\r
96                         {\r
97                                 return trim($out).$end_char;\r
98                         }               \r
99                 }\r
100         }\r
101 }\r
102         \r
103 // ------------------------------------------------------------------------\r
104 \r
105 /**\r
106  * High ASCII to Entities\r
107  *\r
108  * Converts High ascii text and MS Word special characters to character entities\r
109  *\r
110  * @access      public\r
111  * @param       string\r
112  * @return      string\r
113  */     \r
114 if ( ! function_exists('ascii_to_entities'))\r
115 {\r
116         function ascii_to_entities($str)\r
117         {\r
118            $count       = 1;\r
119            $out = '';\r
120            $temp        = array();\r
121         \r
122            for ($i = 0, $s = strlen($str); $i < $s; $i++)\r
123            {\r
124                    $ordinal = ord($str[$i]);\r
125         \r
126                    if ($ordinal < 128)\r
127                    {\r
128                            $out .= $str[$i];\r
129                    }\r
130                    else\r
131                    {\r
132                            if (count($temp) == 0)\r
133                            {\r
134                                    $count = ($ordinal < 224) ? 2 : 3;\r
135                            }\r
136                 \r
137                            $temp[] = $ordinal;\r
138                 \r
139                            if (count($temp) == $count)\r
140                            {\r
141                                    $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);\r
142 \r
143                                    $out .= '&#'.$number.';';\r
144                                    $count = 1;\r
145                                    $temp = array();\r
146                            }\r
147                    }\r
148            }\r
149 \r
150            return $out;\r
151         }\r
152 }\r
153         \r
154 // ------------------------------------------------------------------------\r
155 \r
156 /**\r
157  * Entities to ASCII\r
158  *\r
159  * Converts character entities back to ASCII\r
160  *\r
161  * @access      public\r
162  * @param       string\r
163  * @param       bool\r
164  * @return      string\r
165  */     \r
166 if ( ! function_exists('entities_to_ascii'))\r
167 {\r
168         function entities_to_ascii($str, $all = TRUE)\r
169         {\r
170            if (preg_match_all('/\&#(\d+)\;/', $str, $matches))\r
171            {\r
172                    for ($i = 0, $s = count($matches['0']); $i < $s; $i++)\r
173                    {                            \r
174                            $digits = $matches['1'][$i];\r
175 \r
176                            $out = '';\r
177 \r
178                            if ($digits < 128)\r
179                            {\r
180                                    $out .= chr($digits);\r
181                 \r
182                            }\r
183                            elseif ($digits < 2048)\r
184                            {\r
185                                    $out .= chr(192 + (($digits - ($digits % 64)) / 64));\r
186                                    $out .= chr(128 + ($digits % 64));\r
187                            }\r
188                            else\r
189                            {\r
190                                    $out .= chr(224 + (($digits - ($digits % 4096)) / 4096));\r
191                                    $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));\r
192                                    $out .= chr(128 + ($digits % 64));\r
193                            }\r
194 \r
195                            $str = str_replace($matches['0'][$i], $out, $str);                           \r
196                    }\r
197            }\r
198 \r
199            if ($all)\r
200            {\r
201                    $str = str_replace(array("&amp;", "&lt;", "&gt;", "&quot;", "&apos;", "&#45;"),\r
202                                                           array("&","<",">","\"", "'", "-"),\r
203                                                           $str);\r
204            }\r
205 \r
206            return $str;\r
207         }\r
208 }\r
209         \r
210 // ------------------------------------------------------------------------\r
211 \r
212 /**\r
213  * Word Censoring Function\r
214  *\r
215  * Supply a string and an array of disallowed words and any\r
216  * matched words will be converted to #### or to the replacement\r
217  * word you've submitted.\r
218  *\r
219  * @access      public\r
220  * @param       string  the text string\r
221  * @param       string  the array of censoered words\r
222  * @param       string  the optional replacement value\r
223  * @return      string\r
224  */     \r
225 if ( ! function_exists('word_censor'))\r
226 {\r
227         function word_censor($str, $censored, $replacement = '')\r
228         {\r
229                 if ( ! is_array($censored))\r
230                 {\r
231                         return $str;\r
232                 }\r
233 \r
234                 $str = ' '.$str.' ';\r
235                 foreach ($censored as $badword)\r
236                 {\r
237                         if ($replacement != '')\r
238                         {\r
239                                 $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/i", $replacement, $str);\r
240                         }\r
241                         else\r
242                         {\r
243                                 $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/ie", "str_repeat('#', strlen('\\1'))", $str);\r
244                         }\r
245                 }\r
246         \r
247                 return trim($str);\r
248         }\r
249 }\r
250         \r
251 // ------------------------------------------------------------------------\r
252 \r
253 /**\r
254  * Code Highlighter\r
255  *\r
256  * Colorizes code strings\r
257  *\r
258  * @access      public\r
259  * @param       string  the text string\r
260  * @return      string\r
261  */     \r
262 if ( ! function_exists('highlight_code'))\r
263 {\r
264         function highlight_code($str)\r
265         {               \r
266                 // The highlight string function encodes and highlights\r
267                 // brackets so we need them to start raw\r
268                 $str = str_replace(array('&lt;', '&gt;'), array('<', '>'), $str);\r
269         \r
270                 // Replace any existing PHP tags to temporary markers so they don't accidentally\r
271                 // break the string out of PHP, and thus, thwart the highlighting.\r
272         \r
273                 $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'), \r
274                                                         array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str);\r
275 \r
276                 // The highlight_string function requires that the text be surrounded\r
277                 // by PHP tags, which we will remove later\r
278                 $str = '<?php '.$str.' ?>'; // <?\r
279 \r
280                 // All the magic happens here, baby!    \r
281                 $str = highlight_string($str, TRUE);\r
282 \r
283                 // Prior to PHP 5, the highligh function used icky <font> tags\r
284                 // so we'll replace them with <span> tags.\r
285 \r
286                 if (abs(PHP_VERSION) < 5)\r
287                 {\r
288                         $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);\r
289                         $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);\r
290                 }\r
291                 \r
292                 // Remove our artificially added PHP, and the syntax highlighting that came with it\r
293                 $str = preg_replace('/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i', '<span style="color: #$1">', $str);\r
294                 $str = preg_replace('/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is', "$1</span>\n</span>\n</code>", $str);\r
295                 $str = preg_replace('/<span style="color: #[A-Z0-9]+"\><\/span>/i', '', $str);\r
296                         \r
297                 // Replace our markers back to PHP tags.\r
298                 $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),\r
299                                                         array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'), $str);\r
300                                                                                 \r
301                 return $str;\r
302         }\r
303 }\r
304         \r
305 // ------------------------------------------------------------------------\r
306 \r
307 /**\r
308  * Phrase Highlighter\r
309  *\r
310  * Highlights a phrase within a text string\r
311  *\r
312  * @access      public\r
313  * @param       string  the text string\r
314  * @param       string  the phrase you'd like to highlight\r
315  * @param       string  the openging tag to precede the phrase with\r
316  * @param       string  the closing tag to end the phrase with\r
317  * @return      string\r
318  */     \r
319 if ( ! function_exists('highlight_phrase'))\r
320 {\r
321         function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')\r
322         {\r
323                 if ($str == '')\r
324                 {\r
325                         return '';\r
326                 }\r
327         \r
328                 if ($phrase != '')\r
329                 {\r
330                         return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);\r
331                 }\r
332 \r
333                 return $str;\r
334         }\r
335 }\r
336         \r
337 // ------------------------------------------------------------------------\r
338 \r
339 /**\r
340  * Word Wrap\r
341  *\r
342  * Wraps text at the specified character.  Maintains the integrity of words.\r
343  * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor\r
344  * will URLs.\r
345  *\r
346  * @access      public\r
347  * @param       string  the text string\r
348  * @param       integer the number of characters to wrap at\r
349  * @return      string\r
350  */     \r
351 if ( ! function_exists('word_wrap'))\r
352 {\r
353         function word_wrap($str, $charlim = '76')\r
354         {\r
355                 // Se the character limit\r
356                 if ( ! is_numeric($charlim))\r
357                         $charlim = 76;\r
358         \r
359                 // Reduce multiple spaces\r
360                 $str = preg_replace("| +|", " ", $str);\r
361         \r
362                 // Standardize newlines\r
363                 if (strpos($str, "\r") !== FALSE)\r
364                 {\r
365                         $str = str_replace(array("\r\n", "\r"), "\n", $str);                    \r
366                 }\r
367         \r
368                 // If the current word is surrounded by {unwrap} tags we'll \r
369                 // strip the entire chunk and replace it with a marker.\r
370                 $unwrap = array();\r
371                 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))\r
372                 {\r
373                         for ($i = 0; $i < count($matches['0']); $i++)\r
374                         {\r
375                                 $unwrap[] = $matches['1'][$i];                          \r
376                                 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);\r
377                         }\r
378                 }\r
379         \r
380                 // Use PHP's native function to do the initial wordwrap.  \r
381                 // We set the cut flag to FALSE so that any individual words that are \r
382                 // too long get left alone.  In the next step we'll deal with them.\r
383                 $str = wordwrap($str, $charlim, "\n", FALSE);\r
384         \r
385                 // Split the string into individual lines of text and cycle through them\r
386                 $output = "";\r
387                 foreach (explode("\n", $str) as $line) \r
388                 {\r
389                         // Is the line within the allowed character count?\r
390                         // If so we'll join it to the output and continue\r
391                         if (strlen($line) <= $charlim)\r
392                         {\r
393                                 $output .= $line."\n";                  \r
394                                 continue;\r
395                         }\r
396                         \r
397                         $temp = '';\r
398                         while((strlen($line)) > $charlim) \r
399                         {\r
400                                 // If the over-length word is a URL we won't wrap it\r
401                                 if (preg_match("!\[url.+\]|://|wwww.!", $line))\r
402                                 {\r
403                                         break;\r
404                                 }\r
405 \r
406                                 // Trim the word down\r
407                                 $temp .= substr($line, 0, $charlim-1);\r
408                                 $line = substr($line, $charlim-1);\r
409                         }\r
410                 \r
411                         // If $temp contains data it means we had to split up an over-length \r
412                         // word into smaller chunks so we'll add it back to our current line\r
413                         if ($temp != '')\r
414                         {\r
415                                 $output .= $temp . "\n" . $line; \r
416                         }\r
417                         else\r
418                         {\r
419                                 $output .= $line;\r
420                         }\r
421 \r
422                         $output .= "\n";\r
423                 }\r
424 \r
425                 // Put our markers back\r
426                 if (count($unwrap) > 0)\r
427                 {       \r
428                         foreach ($unwrap as $key => $val)\r
429                         {\r
430                                 $output = str_replace("{{unwrapped".$key."}}", $val, $output);\r
431                         }\r
432                 }\r
433 \r
434                 // Remove the unwrap tags\r
435                 $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output);\r
436 \r
437                 return $output; \r
438         }\r
439 }\r
440 \r
441 \r
442 /* End of file text_helper.php */\r
443 /* Location: ./system/helpers/text_helper.php */