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