From b0dd10f8171945e0c1f3527dd1e9d18b043e01a7 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 25 Aug 2006 17:25:49 +0000 Subject: Initial Import --- system/helpers/text_helper.php | 386 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 386 insertions(+) create mode 100644 system/helpers/text_helper.php (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php new file mode 100644 index 000000000..15b5573bf --- /dev/null +++ b/system/helpers/text_helper.php @@ -0,0 +1,386 @@ += $n) + { + return trim($out).$end_char; + } + } +} + +// ------------------------------------------------------------------------ + +/** + * High ASCII to Entities + * + * Converts High ascii text and MS Word special characters to character entities + * + * @access public + * @param string + * @return string + */ +function ascii_to_entities($str) +{ + $count = 1; + $out = ''; + $temp = array(); + + for ($i = 0, $s = strlen($str); $i < $s; $i++) + { + $ordinal = ord($str[$i]); + + if ($ordinal < 128) + { + $out .= $str[$i]; + } + else + { + if (count($temp) == 0) + { + $count = ($ordinal < 224) ? 2 : 3; + } + + $temp[] = $ordinal; + + if (count($temp) == $count) + { + $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); + + $out .= '&#'.$number.';'; + $count = 1; + $temp = array(); + } + } + } + + return $out; +} + +// ------------------------------------------------------------------------ + +/** + * Entities to ASCII + * + * Converts character entities back to ASCII + * + * @access public + * @param string + * @param bool + * @return string + */ +function entities_to_ascii($str, $all = TRUE) +{ + if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) + { + for ($i = 0, $s = count($matches['0']); $i < $s; $i++) + { + $digits = $matches['1'][$i]; + + $out = ''; + + if ($digits < 128) + { + $out .= chr($digits); + + } + elseif ($digits < 2048) + { + $out .= chr(192 + (($digits - ($digits % 64)) / 64)); + $out .= chr(128 + ($digits % 64)); + } + else + { + $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); + $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); + $out .= chr(128 + ($digits % 64)); + } + + $str = str_replace($matches['0'][$i], $out, $str); + } + } + + if ($all) + { + $str = str_replace(array("&", "<", ">", """, "'", "-"), + array("&","<",">","\"", "'", "-"), + $str); + } + + return $str; +} + +// ------------------------------------------------------------------------ + +/** + * Word Censoring Function + * + * Supply a string and an array of disallowed words and any + * matched words will be converted to #### or to the replacement + * word you've submitted. + * + * @access public + * @param string the text string + * @param string the array of censoered words + * @param string the optional replacement value + * @return string + */ +function word_censor($str, $censored, $replacement = '') +{ + if ( ! is_array($censored)) + { + return $str; + } + + $str = ' '.$str.' '; + foreach ($censored as $badword) + { + if ($replacement != '') + { + $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); + } + else + { + $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); + } + } + + return trim($str); +} + +// ------------------------------------------------------------------------ + +/** + * Code Highlighter + * + * Colorizes code strings + * + * @access public + * @param string the text string + * @return string + */ +function highlight_code($str) +{ + // The highlight string function encodes and highlights + // brackets so we need them to start raw + $str = str_replace(array('<', '>'), array('<', '>'), $str); + + // Replace any existing PHP tags to temporary markers so they don't accidentally + // break the string out of PHP, and thus, thwart the highlighting. + + $str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str); + + // The highlight_string function requires that the text be surrounded + // by PHP tags. Since we don't know if A) the submitted text has PHP tags, + // or B) whether the PHP tags enclose the entire string, we will add our + // own PHP tags around the string along with some markers to make replacement easier later + + $str = ''; // '), array(''), $str); + $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); + } + + // Remove our artificially added PHP + $str = preg_replace("#\.+?//tempstart\
\#is", "\n", $str); + $str = preg_replace("#\.+?//tempstart\
#is", "\n", $str); + $str = preg_replace("#//tempend.+#is", "
\n", $str); + + // Replace our markers back to PHP tags. + $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); //', $tag_close = '') +{ + if ($str == '') + { + return ''; + } + + if ($phrase != '') + { + return preg_replace('/('.preg_quote($phrase).')/i', $tag_open."\\1".$tag_close, $str); + } + + return $str; +} + +// ------------------------------------------------------------------------ + +/** + * Word Wrap + * + * Wraps text at the specified character. Maintains the integrity of words. + * + * @access public + * @param string the text string + * @param integer the number of characters to wrap at + * @return string + */ +function word_wrap($str, $chars = '76') +{ + if ( ! ctype_digit($chars)) + $chars = 76; + + $str = preg_replace("/(\r\n|\r|\n)/", "\n", $str); + $lines = split("\n", $str); + + $output = ""; + while (list(, $thisline) = each($lines)) + { + if (strlen($thisline) > $chars) + { + $line = ""; + $words = split(" ", $thisline); + while(list(, $thisword) = each($words)) + { + while((strlen($thisword)) > $chars) + { + $cur_pos = 0; + for($i=0; $i < $chars - 1; $i++) + { + $output .= $thisword[$i]; + $cur_pos++; + } + + $output .= "\n"; + $thisword = substr($thisword, $cur_pos, (strlen($thisword) - $cur_pos)); + } + + if ((strlen($line) + strlen($thisword)) > $chars) + { + $output .= $line."\n"; + $line = $thisword." "; + } + else + { + $line .= $thisword." "; + } + } + + $output .= $line."\n"; + } + else + { + $output .= $thisline."\n"; + } + } + + return $output; +} + +?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 1cf89aab5fff8c8068cbf0ed18038b6e4fd4f605 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 3 Sep 2006 18:24:39 +0000 Subject: --- system/helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 15b5573bf..e4f816eb1 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -333,7 +333,7 @@ function highlight_phrase($str, $phrase, $tag_open = '', $tag_close = '< */ function word_wrap($str, $chars = '76') { - if ( ! ctype_digit($chars)) + if ( ! is_numeric($chars)) $chars = 76; $str = preg_replace("/(\r\n|\r|\n)/", "\n", $str); -- cgit v1.2.3-24-g4f1b From bc042dd0692a6b4b89f11f88e6e3763162ce8048 Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 21 Sep 2006 02:46:59 +0000 Subject: --- system/helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index e4f816eb1..e2da32746 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -266,7 +266,7 @@ function highlight_code($str) // or B) whether the PHP tags enclose the entire string, we will add our // own PHP tags around the string along with some markers to make replacement easier later - $str = ''; // '; // All the magic happens here, baby! $str = highlight_string($str, TRUE); -- cgit v1.2.3-24-g4f1b From e334c472fb4be44feec3a73402fc4a2b062cbfc0 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 21 Oct 2006 19:44:22 +0000 Subject: --- system/helpers/text_helper.php | 68 +++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 34 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index e2da32746..fbc63a9b4 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -7,12 +7,12 @@ * @package CodeIgniter * @author Rick Ellis * @copyright Copyright (c) 2006, pMachine, Inc. - * @license http://www.codeignitor.com/user_guide/license.html + * @license http://www.codeignitor.com/user_guide/license.html * @link http://www.codeigniter.com * @since Version 1.0 * @filesource */ - + // ------------------------------------------------------------------------ /** @@ -35,12 +35,12 @@ * @access public * @param string * @param integer - * @param string the end character. Usually an ellipsis + * @param string the end character. Usually an ellipsis * @return string */ function word_limiter($str, $n = 100, $end_char = '…') { - if (strlen($str) < $n) + if (strlen($str) < $n) { return $str; } @@ -53,12 +53,12 @@ function word_limiter($str, $n = 100, $end_char = '…') } $str = ''; - for ($i = 0; $i < $n; $i++) + for ($i = 0; $i < $n; $i++) { $str .= $words[$i].' '; } - return trim($str).$end_char; + return trim($str).$end_char; } // ------------------------------------------------------------------------ @@ -72,12 +72,12 @@ function word_limiter($str, $n = 100, $end_char = '…') * @access public * @param string * @param integer - * @param string the end character. Usually an ellipsis + * @param string the end character. Usually an ellipsis * @return string */ function character_limiter($str, $n = 500, $end_char = '…') { - if (strlen($str) < $n) + if (strlen($str) < $n) { return $str; } @@ -95,7 +95,7 @@ function character_limiter($str, $n = 500, $end_char = '…') $out .= $val.' '; if (strlen($out) >= $n) { - return trim($out).$end_char; + return trim($out).$end_char; } } } @@ -116,14 +116,14 @@ function ascii_to_entities($str) $count = 1; $out = ''; $temp = array(); - + for ($i = 0, $s = strlen($str); $i < $s; $i++) { $ordinal = ord($str[$i]); - + if ($ordinal < 128) { - $out .= $str[$i]; + $out .= $str[$i]; } else { @@ -131,9 +131,9 @@ function ascii_to_entities($str) { $count = ($ordinal < 224) ? 2 : 3; } - + $temp[] = $ordinal; - + if (count($temp) == $count) { $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); @@ -141,10 +141,10 @@ function ascii_to_entities($str) $out .= '&#'.$number.';'; $count = 1; $temp = array(); - } - } + } + } } - + return $out; } @@ -169,35 +169,35 @@ function entities_to_ascii($str, $all = TRUE) $digits = $matches['1'][$i]; $out = ''; - + if ($digits < 128) { $out .= chr($digits); - - } + + } elseif ($digits < 2048) { $out .= chr(192 + (($digits - ($digits % 64)) / 64)); $out .= chr(128 + ($digits % 64)); - } + } else { $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); $out .= chr(128 + ($digits % 64)); } - + $str = str_replace($matches['0'][$i], $out, $str); } } - + if ($all) { $str = str_replace(array("&", "<", ">", """, "'", "-"), array("&","<",">","\"", "'", "-"), $str); } - + return $str; } @@ -252,8 +252,8 @@ function word_censor($str, $censored, $replacement = '') */ function highlight_code($str) { - // The highlight string function encodes and highlights - // brackets so we need them to start raw + // The highlight string function encodes and highlights + // brackets so we need them to start raw $str = str_replace(array('<', '>'), array('<', '>'), $str); // Replace any existing PHP tags to temporary markers so they don't accidentally @@ -340,15 +340,15 @@ function word_wrap($str, $chars = '76') $lines = split("\n", $str); $output = ""; - while (list(, $thisline) = each($lines)) + while (list(, $thisline) = each($lines)) { if (strlen($thisline) > $chars) { $line = ""; $words = split(" ", $thisline); - while(list(, $thisword) = each($words)) + while(list(, $thisword) = each($words)) { - while((strlen($thisword)) > $chars) + while((strlen($thisword)) > $chars) { $cur_pos = 0; for($i=0; $i < $chars - 1; $i++) @@ -361,20 +361,20 @@ function word_wrap($str, $chars = '76') $thisword = substr($thisword, $cur_pos, (strlen($thisword) - $cur_pos)); } - if ((strlen($line) + strlen($thisword)) > $chars) + if ((strlen($line) + strlen($thisword)) > $chars) { $output .= $line."\n"; $line = $thisword." "; - } - else + } + else { $line .= $thisword." "; } } $output .= $line."\n"; - } - else + } + else { $output .= $thisline."\n"; } -- cgit v1.2.3-24-g4f1b From 86c60398852a299aa4a485c7e9e3c1d45e8c2184 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 28 Oct 2006 02:50:53 +0000 Subject: --- system/helpers/text_helper.php | 105 +++++++++++++++++++++++++++-------------- 1 file changed, 70 insertions(+), 35 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index fbc63a9b4..1b9178129 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -325,62 +325,97 @@ function highlight_phrase($str, $phrase, $tag_open = '', $tag_close = '< * Word Wrap * * Wraps text at the specified character. Maintains the integrity of words. + * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor + * will URLs. * * @access public * @param string the text string * @param integer the number of characters to wrap at * @return string */ -function word_wrap($str, $chars = '76') -{ - if ( ! is_numeric($chars)) - $chars = 76; +function word_wrap($str, $charlim = '76') +{ + // Se the character limit + if ( ! is_numeric($charlim)) + $charlim = 76; + + // Reduce multiple spaces + $str = preg_replace("| +|", " ", $str); - $str = preg_replace("/(\r\n|\r|\n)/", "\n", $str); - $lines = split("\n", $str); + // Standardize newlines + $str = preg_replace("/\r\n|\r/", "\n", $str); + // If the current word is surrounded by {unwrap} tags we'll + // strip the entire chunk and replace it with a marker. + $unwrap = array(); + if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) + { + for ($i = 0; $i < count($matches['0']); $i++) + { + $unwrap[] = $matches['1'][$i]; + $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); + } + } + + // Use PHP's native function to do the initial wordwrap. + // We set the cut flag to FALSE so that any individual words that are + // too long get left alone. In the next step we'll deal with them. + $str = wordwrap($str, $charlim, "\n", FALSE); + + // Split the string into individual lines of text and cycle through them $output = ""; - while (list(, $thisline) = each($lines)) + foreach (explode("\n", $str) as $line) { - if (strlen($thisline) > $chars) + // Is the line within the allowed character count? + // If so we'll join it to the output and continue + if (strlen($line) <= $charlim) + { + $output .= $line.$this->newline; + continue; + } + + $temp = ''; + while((strlen($line)) > $charlim) { - $line = ""; - $words = split(" ", $thisline); - while(list(, $thisword) = each($words)) + // If the over-length word is a URL we won't wrap it + if (preg_match("!\[url.+\]|://|wwww.!", $line)) { - while((strlen($thisword)) > $chars) - { - $cur_pos = 0; - for($i=0; $i < $chars - 1; $i++) - { - $output .= $thisword[$i]; - $cur_pos++; - } - - $output .= "\n"; - $thisword = substr($thisword, $cur_pos, (strlen($thisword) - $cur_pos)); - } - - if ((strlen($line) + strlen($thisword)) > $chars) - { - $output .= $line."\n"; - $line = $thisword." "; - } - else - { - $line .= $thisword." "; - } + break; } - $output .= $line."\n"; + // Trim the word down + $temp .= substr($line, 0, $charlim-1); + $line = substr($line, $charlim-1); + } + + // If $temp contains data it means we had to split up an over-length + // word into smaller chunks so we'll add it back to our current line + if ($temp != '') + { + $output .= $temp.$this->newline.$line; } else { - $output .= $thisline."\n"; + $output .= $line; + } + + $output .= $this->newline; + } + + // Put our markers back + if (count($unwrap) > 0) + { + foreach ($unwrap as $key => $val) + { + $output = str_replace("{{unwrapped".$key."}}", $val, $output); } } + // Remove the unwrap tags + $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); + return $output; } + ?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 7c807cba83ab368d027a28f4b84ee6a301430e80 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 31 Oct 2006 18:13:24 +0000 Subject: --- system/helpers/text_helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 1b9178129..655d6e531 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -370,7 +370,7 @@ function word_wrap($str, $charlim = '76') // If so we'll join it to the output and continue if (strlen($line) <= $charlim) { - $output .= $line.$this->newline; + $output .= $line."\n"; continue; } @@ -399,7 +399,7 @@ function word_wrap($str, $charlim = '76') $output .= $line; } - $output .= $this->newline; + $output .= "\n"; } // Put our markers back -- cgit v1.2.3-24-g4f1b From d2df9bc7cc9d4b3e53818470c5d0977c9a36677c Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Sun, 15 Apr 2007 17:41:17 +0000 Subject: update pMachine to EllisLab update copyright year update Code Igniter to CodeIgniter --- system/helpers/text_helper.php | 840 ++++++++++++++++++++--------------------- 1 file changed, 420 insertions(+), 420 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 655d6e531..3c7270b9b 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -1,421 +1,421 @@ -= $n) - { - return trim($out).$end_char; - } - } -} - -// ------------------------------------------------------------------------ - -/** - * High ASCII to Entities - * - * Converts High ascii text and MS Word special characters to character entities - * - * @access public - * @param string - * @return string - */ -function ascii_to_entities($str) -{ - $count = 1; - $out = ''; - $temp = array(); - - for ($i = 0, $s = strlen($str); $i < $s; $i++) - { - $ordinal = ord($str[$i]); - - if ($ordinal < 128) - { - $out .= $str[$i]; - } - else - { - if (count($temp) == 0) - { - $count = ($ordinal < 224) ? 2 : 3; - } - - $temp[] = $ordinal; - - if (count($temp) == $count) - { - $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); - - $out .= '&#'.$number.';'; - $count = 1; - $temp = array(); - } - } - } - - return $out; -} - -// ------------------------------------------------------------------------ - -/** - * Entities to ASCII - * - * Converts character entities back to ASCII - * - * @access public - * @param string - * @param bool - * @return string - */ -function entities_to_ascii($str, $all = TRUE) -{ - if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) - { - for ($i = 0, $s = count($matches['0']); $i < $s; $i++) - { - $digits = $matches['1'][$i]; - - $out = ''; - - if ($digits < 128) - { - $out .= chr($digits); - - } - elseif ($digits < 2048) - { - $out .= chr(192 + (($digits - ($digits % 64)) / 64)); - $out .= chr(128 + ($digits % 64)); - } - else - { - $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); - $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); - $out .= chr(128 + ($digits % 64)); - } - - $str = str_replace($matches['0'][$i], $out, $str); - } - } - - if ($all) - { - $str = str_replace(array("&", "<", ">", """, "'", "-"), - array("&","<",">","\"", "'", "-"), - $str); - } - - return $str; -} - -// ------------------------------------------------------------------------ - -/** - * Word Censoring Function - * - * Supply a string and an array of disallowed words and any - * matched words will be converted to #### or to the replacement - * word you've submitted. - * - * @access public - * @param string the text string - * @param string the array of censoered words - * @param string the optional replacement value - * @return string - */ -function word_censor($str, $censored, $replacement = '') -{ - if ( ! is_array($censored)) - { - return $str; - } - - $str = ' '.$str.' '; - foreach ($censored as $badword) - { - if ($replacement != '') - { - $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); - } - else - { - $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); - } - } - - return trim($str); -} - -// ------------------------------------------------------------------------ - -/** - * Code Highlighter - * - * Colorizes code strings - * - * @access public - * @param string the text string - * @return string - */ -function highlight_code($str) -{ - // The highlight string function encodes and highlights - // brackets so we need them to start raw - $str = str_replace(array('<', '>'), array('<', '>'), $str); - - // Replace any existing PHP tags to temporary markers so they don't accidentally - // break the string out of PHP, and thus, thwart the highlighting. - - $str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str); - - // The highlight_string function requires that the text be surrounded - // by PHP tags. Since we don't know if A) the submitted text has PHP tags, - // or B) whether the PHP tags enclose the entire string, we will add our - // own PHP tags around the string along with some markers to make replacement easier later - - $str = ''; - - // All the magic happens here, baby! - $str = highlight_string($str, TRUE); - - // Prior to PHP 5, the highlight function used icky font tags - // so we'll replace them with span tags. - if (abs(phpversion()) < 5) - { - $str = str_replace(array(''), array(''), $str); - $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); - } - - // Remove our artificially added PHP - $str = preg_replace("#\.+?//tempstart\
\#is", "\n", $str); - $str = preg_replace("#\.+?//tempstart\
#is", "\n", $str); - $str = preg_replace("#//tempend.+#is", "
\n", $str); - - // Replace our markers back to PHP tags. - $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); //', $tag_close = '
') -{ - if ($str == '') - { - return ''; - } - - if ($phrase != '') - { - return preg_replace('/('.preg_quote($phrase).')/i', $tag_open."\\1".$tag_close, $str); - } - - return $str; -} - -// ------------------------------------------------------------------------ - -/** - * Word Wrap - * - * Wraps text at the specified character. Maintains the integrity of words. - * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor - * will URLs. - * - * @access public - * @param string the text string - * @param integer the number of characters to wrap at - * @return string - */ -function word_wrap($str, $charlim = '76') -{ - // Se the character limit - if ( ! is_numeric($charlim)) - $charlim = 76; - - // Reduce multiple spaces - $str = preg_replace("| +|", " ", $str); - - // Standardize newlines - $str = preg_replace("/\r\n|\r/", "\n", $str); - - // If the current word is surrounded by {unwrap} tags we'll - // strip the entire chunk and replace it with a marker. - $unwrap = array(); - if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) - { - for ($i = 0; $i < count($matches['0']); $i++) - { - $unwrap[] = $matches['1'][$i]; - $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); - } - } - - // Use PHP's native function to do the initial wordwrap. - // We set the cut flag to FALSE so that any individual words that are - // too long get left alone. In the next step we'll deal with them. - $str = wordwrap($str, $charlim, "\n", FALSE); - - // Split the string into individual lines of text and cycle through them - $output = ""; - foreach (explode("\n", $str) as $line) - { - // Is the line within the allowed character count? - // If so we'll join it to the output and continue - if (strlen($line) <= $charlim) - { - $output .= $line."\n"; - continue; - } - - $temp = ''; - while((strlen($line)) > $charlim) - { - // If the over-length word is a URL we won't wrap it - if (preg_match("!\[url.+\]|://|wwww.!", $line)) - { - break; - } - - // Trim the word down - $temp .= substr($line, 0, $charlim-1); - $line = substr($line, $charlim-1); - } - - // If $temp contains data it means we had to split up an over-length - // word into smaller chunks so we'll add it back to our current line - if ($temp != '') - { - $output .= $temp.$this->newline.$line; - } - else - { - $output .= $line; - } - - $output .= "\n"; - } - - // Put our markers back - if (count($unwrap) > 0) - { - foreach ($unwrap as $key => $val) - { - $output = str_replace("{{unwrapped".$key."}}", $val, $output); - } - } - - // Remove the unwrap tags - $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); - - return $output; -} - - += $n) + { + return trim($out).$end_char; + } + } +} + +// ------------------------------------------------------------------------ + +/** + * High ASCII to Entities + * + * Converts High ascii text and MS Word special characters to character entities + * + * @access public + * @param string + * @return string + */ +function ascii_to_entities($str) +{ + $count = 1; + $out = ''; + $temp = array(); + + for ($i = 0, $s = strlen($str); $i < $s; $i++) + { + $ordinal = ord($str[$i]); + + if ($ordinal < 128) + { + $out .= $str[$i]; + } + else + { + if (count($temp) == 0) + { + $count = ($ordinal < 224) ? 2 : 3; + } + + $temp[] = $ordinal; + + if (count($temp) == $count) + { + $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); + + $out .= '&#'.$number.';'; + $count = 1; + $temp = array(); + } + } + } + + return $out; +} + +// ------------------------------------------------------------------------ + +/** + * Entities to ASCII + * + * Converts character entities back to ASCII + * + * @access public + * @param string + * @param bool + * @return string + */ +function entities_to_ascii($str, $all = TRUE) +{ + if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) + { + for ($i = 0, $s = count($matches['0']); $i < $s; $i++) + { + $digits = $matches['1'][$i]; + + $out = ''; + + if ($digits < 128) + { + $out .= chr($digits); + + } + elseif ($digits < 2048) + { + $out .= chr(192 + (($digits - ($digits % 64)) / 64)); + $out .= chr(128 + ($digits % 64)); + } + else + { + $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); + $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); + $out .= chr(128 + ($digits % 64)); + } + + $str = str_replace($matches['0'][$i], $out, $str); + } + } + + if ($all) + { + $str = str_replace(array("&", "<", ">", """, "'", "-"), + array("&","<",">","\"", "'", "-"), + $str); + } + + return $str; +} + +// ------------------------------------------------------------------------ + +/** + * Word Censoring Function + * + * Supply a string and an array of disallowed words and any + * matched words will be converted to #### or to the replacement + * word you've submitted. + * + * @access public + * @param string the text string + * @param string the array of censoered words + * @param string the optional replacement value + * @return string + */ +function word_censor($str, $censored, $replacement = '') +{ + if ( ! is_array($censored)) + { + return $str; + } + + $str = ' '.$str.' '; + foreach ($censored as $badword) + { + if ($replacement != '') + { + $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); + } + else + { + $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); + } + } + + return trim($str); +} + +// ------------------------------------------------------------------------ + +/** + * Code Highlighter + * + * Colorizes code strings + * + * @access public + * @param string the text string + * @return string + */ +function highlight_code($str) +{ + // The highlight string function encodes and highlights + // brackets so we need them to start raw + $str = str_replace(array('<', '>'), array('<', '>'), $str); + + // Replace any existing PHP tags to temporary markers so they don't accidentally + // break the string out of PHP, and thus, thwart the highlighting. + + $str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str); + + // The highlight_string function requires that the text be surrounded + // by PHP tags. Since we don't know if A) the submitted text has PHP tags, + // or B) whether the PHP tags enclose the entire string, we will add our + // own PHP tags around the string along with some markers to make replacement easier later + + $str = ''; + + // All the magic happens here, baby! + $str = highlight_string($str, TRUE); + + // Prior to PHP 5, the highlight function used icky font tags + // so we'll replace them with span tags. + if (abs(phpversion()) < 5) + { + $str = str_replace(array(''), array(''), $str); + $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); + } + + // Remove our artificially added PHP + $str = preg_replace("#\.+?//tempstart\
\#is", "\n", $str); + $str = preg_replace("#\.+?//tempstart\
#is", "\n", $str); + $str = preg_replace("#//tempend.+#is", "
\n", $str); + + // Replace our markers back to PHP tags. + $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); //', $tag_close = '
') +{ + if ($str == '') + { + return ''; + } + + if ($phrase != '') + { + return preg_replace('/('.preg_quote($phrase).')/i', $tag_open."\\1".$tag_close, $str); + } + + return $str; +} + +// ------------------------------------------------------------------------ + +/** + * Word Wrap + * + * Wraps text at the specified character. Maintains the integrity of words. + * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor + * will URLs. + * + * @access public + * @param string the text string + * @param integer the number of characters to wrap at + * @return string + */ +function word_wrap($str, $charlim = '76') +{ + // Se the character limit + if ( ! is_numeric($charlim)) + $charlim = 76; + + // Reduce multiple spaces + $str = preg_replace("| +|", " ", $str); + + // Standardize newlines + $str = preg_replace("/\r\n|\r/", "\n", $str); + + // If the current word is surrounded by {unwrap} tags we'll + // strip the entire chunk and replace it with a marker. + $unwrap = array(); + if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) + { + for ($i = 0; $i < count($matches['0']); $i++) + { + $unwrap[] = $matches['1'][$i]; + $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); + } + } + + // Use PHP's native function to do the initial wordwrap. + // We set the cut flag to FALSE so that any individual words that are + // too long get left alone. In the next step we'll deal with them. + $str = wordwrap($str, $charlim, "\n", FALSE); + + // Split the string into individual lines of text and cycle through them + $output = ""; + foreach (explode("\n", $str) as $line) + { + // Is the line within the allowed character count? + // If so we'll join it to the output and continue + if (strlen($line) <= $charlim) + { + $output .= $line."\n"; + continue; + } + + $temp = ''; + while((strlen($line)) > $charlim) + { + // If the over-length word is a URL we won't wrap it + if (preg_match("!\[url.+\]|://|wwww.!", $line)) + { + break; + } + + // Trim the word down + $temp .= substr($line, 0, $charlim-1); + $line = substr($line, $charlim-1); + } + + // If $temp contains data it means we had to split up an over-length + // word into smaller chunks so we'll add it back to our current line + if ($temp != '') + { + $output .= $temp.$this->newline.$line; + } + else + { + $output .= $line; + } + + $output .= "\n"; + } + + // Put our markers back + if (count($unwrap) > 0) + { + foreach ($unwrap as $key => $val) + { + $output = str_replace("{{unwrapped".$key."}}", $val, $output); + } + } + + // Remove the unwrap tags + $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); + + return $output; +} + + ?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 6838f00a708f53f834fb8a98af560177db1d1454 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Thu, 4 Oct 2007 19:29:59 +0000 Subject: Fixed a typo in the docblock comments that had CodeIgniter spelled CodeIgnitor. --- system/helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 3c7270b9b..d315db0ff 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -7,7 +7,7 @@ * @package CodeIgniter * @author Rick Ellis * @copyright Copyright (c) 2006, EllisLab, Inc. - * @license http://www.codeignitor.com/user_guide/license.html + * @license http://www.codeigniter.com/user_guide/license.html * @link http://www.codeigniter.com * @since Version 1.0 * @filesource -- cgit v1.2.3-24-g4f1b From 1a274a0c5971086ed1690094790ad12858a73fd4 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Fri, 30 Nov 2007 01:34:12 +0000 Subject: Fixed a bug in word_wrap() of the Text Helper that incorrectly referenced an object. --- system/helpers/text_helper.php | 422 +---------------------------------------- 1 file changed, 1 insertion(+), 421 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index d315db0ff..fb50ba9d7 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -1,421 +1 @@ -= $n) - { - return trim($out).$end_char; - } - } -} - -// ------------------------------------------------------------------------ - -/** - * High ASCII to Entities - * - * Converts High ascii text and MS Word special characters to character entities - * - * @access public - * @param string - * @return string - */ -function ascii_to_entities($str) -{ - $count = 1; - $out = ''; - $temp = array(); - - for ($i = 0, $s = strlen($str); $i < $s; $i++) - { - $ordinal = ord($str[$i]); - - if ($ordinal < 128) - { - $out .= $str[$i]; - } - else - { - if (count($temp) == 0) - { - $count = ($ordinal < 224) ? 2 : 3; - } - - $temp[] = $ordinal; - - if (count($temp) == $count) - { - $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); - - $out .= '&#'.$number.';'; - $count = 1; - $temp = array(); - } - } - } - - return $out; -} - -// ------------------------------------------------------------------------ - -/** - * Entities to ASCII - * - * Converts character entities back to ASCII - * - * @access public - * @param string - * @param bool - * @return string - */ -function entities_to_ascii($str, $all = TRUE) -{ - if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) - { - for ($i = 0, $s = count($matches['0']); $i < $s; $i++) - { - $digits = $matches['1'][$i]; - - $out = ''; - - if ($digits < 128) - { - $out .= chr($digits); - - } - elseif ($digits < 2048) - { - $out .= chr(192 + (($digits - ($digits % 64)) / 64)); - $out .= chr(128 + ($digits % 64)); - } - else - { - $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); - $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); - $out .= chr(128 + ($digits % 64)); - } - - $str = str_replace($matches['0'][$i], $out, $str); - } - } - - if ($all) - { - $str = str_replace(array("&", "<", ">", """, "'", "-"), - array("&","<",">","\"", "'", "-"), - $str); - } - - return $str; -} - -// ------------------------------------------------------------------------ - -/** - * Word Censoring Function - * - * Supply a string and an array of disallowed words and any - * matched words will be converted to #### or to the replacement - * word you've submitted. - * - * @access public - * @param string the text string - * @param string the array of censoered words - * @param string the optional replacement value - * @return string - */ -function word_censor($str, $censored, $replacement = '') -{ - if ( ! is_array($censored)) - { - return $str; - } - - $str = ' '.$str.' '; - foreach ($censored as $badword) - { - if ($replacement != '') - { - $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); - } - else - { - $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); - } - } - - return trim($str); -} - -// ------------------------------------------------------------------------ - -/** - * Code Highlighter - * - * Colorizes code strings - * - * @access public - * @param string the text string - * @return string - */ -function highlight_code($str) -{ - // The highlight string function encodes and highlights - // brackets so we need them to start raw - $str = str_replace(array('<', '>'), array('<', '>'), $str); - - // Replace any existing PHP tags to temporary markers so they don't accidentally - // break the string out of PHP, and thus, thwart the highlighting. - - $str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str); - - // The highlight_string function requires that the text be surrounded - // by PHP tags. Since we don't know if A) the submitted text has PHP tags, - // or B) whether the PHP tags enclose the entire string, we will add our - // own PHP tags around the string along with some markers to make replacement easier later - - $str = ''; - - // All the magic happens here, baby! - $str = highlight_string($str, TRUE); - - // Prior to PHP 5, the highlight function used icky font tags - // so we'll replace them with span tags. - if (abs(phpversion()) < 5) - { - $str = str_replace(array(''), array(''), $str); - $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); - } - - // Remove our artificially added PHP - $str = preg_replace("#\.+?//tempstart\
\#is", "\n", $str); - $str = preg_replace("#\.+?//tempstart\
#is", "\n", $str); - $str = preg_replace("#//tempend.+#is", "
\n", $str); - - // Replace our markers back to PHP tags. - $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); //', $tag_close = '') -{ - if ($str == '') - { - return ''; - } - - if ($phrase != '') - { - return preg_replace('/('.preg_quote($phrase).')/i', $tag_open."\\1".$tag_close, $str); - } - - return $str; -} - -// ------------------------------------------------------------------------ - -/** - * Word Wrap - * - * Wraps text at the specified character. Maintains the integrity of words. - * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor - * will URLs. - * - * @access public - * @param string the text string - * @param integer the number of characters to wrap at - * @return string - */ -function word_wrap($str, $charlim = '76') -{ - // Se the character limit - if ( ! is_numeric($charlim)) - $charlim = 76; - - // Reduce multiple spaces - $str = preg_replace("| +|", " ", $str); - - // Standardize newlines - $str = preg_replace("/\r\n|\r/", "\n", $str); - - // If the current word is surrounded by {unwrap} tags we'll - // strip the entire chunk and replace it with a marker. - $unwrap = array(); - if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) - { - for ($i = 0; $i < count($matches['0']); $i++) - { - $unwrap[] = $matches['1'][$i]; - $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); - } - } - - // Use PHP's native function to do the initial wordwrap. - // We set the cut flag to FALSE so that any individual words that are - // too long get left alone. In the next step we'll deal with them. - $str = wordwrap($str, $charlim, "\n", FALSE); - - // Split the string into individual lines of text and cycle through them - $output = ""; - foreach (explode("\n", $str) as $line) - { - // Is the line within the allowed character count? - // If so we'll join it to the output and continue - if (strlen($line) <= $charlim) - { - $output .= $line."\n"; - continue; - } - - $temp = ''; - while((strlen($line)) > $charlim) - { - // If the over-length word is a URL we won't wrap it - if (preg_match("!\[url.+\]|://|wwww.!", $line)) - { - break; - } - - // Trim the word down - $temp .= substr($line, 0, $charlim-1); - $line = substr($line, $charlim-1); - } - - // If $temp contains data it means we had to split up an over-length - // word into smaller chunks so we'll add it back to our current line - if ($temp != '') - { - $output .= $temp.$this->newline.$line; - } - else - { - $output .= $line; - } - - $output .= "\n"; - } - - // Put our markers back - if (count($unwrap) > 0) - { - foreach ($unwrap as $key => $val) - { - $output = str_replace("{{unwrapped".$key."}}", $val, $output); - } - } - - // Remove the unwrap tags - $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); - - return $output; -} - - -?> \ No newline at end of file += $n) { return trim($out).$end_char; } } } // ------------------------------------------------------------------------ /** * High ASCII to Entities * * Converts High ascii text and MS Word special characters to character entities * * @access public * @param string * @return string */ function ascii_to_entities($str) { $count = 1; $out = ''; $temp = array(); for ($i = 0, $s = strlen($str); $i < $s; $i++) { $ordinal = ord($str[$i]); if ($ordinal < 128) { $out .= $str[$i]; } else { if (count($temp) == 0) { $count = ($ordinal < 224) ? 2 : 3; } $temp[] = $ordinal; if (count($temp) == $count) { $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); $out .= '&#'.$number.';'; $count = 1; $temp = array(); } } } return $out; } // ------------------------------------------------------------------------ /** * Entities to ASCII * * Converts character entities back to ASCII * * @access public * @param string * @param bool * @return string */ function entities_to_ascii($str, $all = TRUE) { if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) { for ($i = 0, $s = count($matches['0']); $i < $s; $i++) { $digits = $matches['1'][$i]; $out = ''; if ($digits < 128) { $out .= chr($digits); } elseif ($digits < 2048) { $out .= chr(192 + (($digits - ($digits % 64)) / 64)); $out .= chr(128 + ($digits % 64)); } else { $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); $out .= chr(128 + ($digits % 64)); } $str = str_replace($matches['0'][$i], $out, $str); } } if ($all) { $str = str_replace(array("&", "<", ">", """, "'", "-"), array("&","<",">","\"", "'", "-"), $str); } return $str; } // ------------------------------------------------------------------------ /** * Word Censoring Function * * Supply a string and an array of disallowed words and any * matched words will be converted to #### or to the replacement * word you've submitted. * * @access public * @param string the text string * @param string the array of censoered words * @param string the optional replacement value * @return string */ function word_censor($str, $censored, $replacement = '') { if ( ! is_array($censored)) { return $str; } $str = ' '.$str.' '; foreach ($censored as $badword) { if ($replacement != '') { $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); } else { $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); } } return trim($str); } // ------------------------------------------------------------------------ /** * Code Highlighter * * Colorizes code strings * * @access public * @param string the text string * @return string */ function highlight_code($str) { // The highlight string function encodes and highlights // brackets so we need them to start raw $str = str_replace(array('<', '>'), array('<', '>'), $str); // Replace any existing PHP tags to temporary markers so they don't accidentally // break the string out of PHP, and thus, thwart the highlighting. $str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str); // The highlight_string function requires that the text be surrounded // by PHP tags. Since we don't know if A) the submitted text has PHP tags, // or B) whether the PHP tags enclose the entire string, we will add our // own PHP tags around the string along with some markers to make replacement easier later $str = ''; // All the magic happens here, baby! $str = highlight_string($str, TRUE); // Prior to PHP 5, the highlight function used icky font tags // so we'll replace them with span tags. if (abs(phpversion()) < 5) { $str = str_replace(array(''), array(''), $str); $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); } // Remove our artificially added PHP $str = preg_replace("#\.+?//tempstart\
\#is", "\n", $str); $str = preg_replace("#\.+?//tempstart\
#is", "\n", $str); $str = preg_replace("#//tempend.+#is", "
\n", $str); // Replace our markers back to PHP tags. $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); //', $tag_close = '') { if ($str == '') { return ''; } if ($phrase != '') { return preg_replace('/('.preg_quote($phrase).')/i', $tag_open."\\1".$tag_close, $str); } return $str; } // ------------------------------------------------------------------------ /** * Word Wrap * * Wraps text at the specified character. Maintains the integrity of words. * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor * will URLs. * * @access public * @param string the text string * @param integer the number of characters to wrap at * @return string */ function word_wrap($str, $charlim = '76') { // Se the character limit if ( ! is_numeric($charlim)) $charlim = 76; // Reduce multiple spaces $str = preg_replace("| +|", " ", $str); // Standardize newlines $str = preg_replace("/\r\n|\r/", "\n", $str); // If the current word is surrounded by {unwrap} tags we'll // strip the entire chunk and replace it with a marker. $unwrap = array(); if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) { for ($i = 0; $i < count($matches['0']); $i++) { $unwrap[] = $matches['1'][$i]; $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); } } // Use PHP's native function to do the initial wordwrap. // We set the cut flag to FALSE so that any individual words that are // too long get left alone. In the next step we'll deal with them. $str = wordwrap($str, $charlim, "\n", FALSE); // Split the string into individual lines of text and cycle through them $output = ""; foreach (explode("\n", $str) as $line) { // Is the line within the allowed character count? // If so we'll join it to the output and continue if (strlen($line) <= $charlim) { $output .= $line."\n"; continue; } $temp = ''; while((strlen($line)) > $charlim) { // If the over-length word is a URL we won't wrap it if (preg_match("!\[url.+\]|://|wwww.!", $line)) { break; } // Trim the word down $temp .= substr($line, 0, $charlim-1); $line = substr($line, $charlim-1); } // If $temp contains data it means we had to split up an over-length // word into smaller chunks so we'll add it back to our current line if ($temp != '') { $output .= $temp . "\n" . $line; } else { $output .= $line; } $output .= "\n"; } // Put our markers back if (count($unwrap) > 0) { foreach ($unwrap as $key => $val) { $output = str_replace("{{unwrapped".$key."}}", $val, $output); } } // Remove the unwrap tags $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); return $output; } ?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b From fc2f834bf9b134994ef1214e06153f74f8772630 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Tue, 11 Dec 2007 13:44:20 +0000 Subject: Fixed a bug in highlight_pharse() that caused an error with slashes. --- system/helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index fb50ba9d7..b75cd8f78 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -1 +1 @@ -= $n) { return trim($out).$end_char; } } } // ------------------------------------------------------------------------ /** * High ASCII to Entities * * Converts High ascii text and MS Word special characters to character entities * * @access public * @param string * @return string */ function ascii_to_entities($str) { $count = 1; $out = ''; $temp = array(); for ($i = 0, $s = strlen($str); $i < $s; $i++) { $ordinal = ord($str[$i]); if ($ordinal < 128) { $out .= $str[$i]; } else { if (count($temp) == 0) { $count = ($ordinal < 224) ? 2 : 3; } $temp[] = $ordinal; if (count($temp) == $count) { $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); $out .= '&#'.$number.';'; $count = 1; $temp = array(); } } } return $out; } // ------------------------------------------------------------------------ /** * Entities to ASCII * * Converts character entities back to ASCII * * @access public * @param string * @param bool * @return string */ function entities_to_ascii($str, $all = TRUE) { if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) { for ($i = 0, $s = count($matches['0']); $i < $s; $i++) { $digits = $matches['1'][$i]; $out = ''; if ($digits < 128) { $out .= chr($digits); } elseif ($digits < 2048) { $out .= chr(192 + (($digits - ($digits % 64)) / 64)); $out .= chr(128 + ($digits % 64)); } else { $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); $out .= chr(128 + ($digits % 64)); } $str = str_replace($matches['0'][$i], $out, $str); } } if ($all) { $str = str_replace(array("&", "<", ">", """, "'", "-"), array("&","<",">","\"", "'", "-"), $str); } return $str; } // ------------------------------------------------------------------------ /** * Word Censoring Function * * Supply a string and an array of disallowed words and any * matched words will be converted to #### or to the replacement * word you've submitted. * * @access public * @param string the text string * @param string the array of censoered words * @param string the optional replacement value * @return string */ function word_censor($str, $censored, $replacement = '') { if ( ! is_array($censored)) { return $str; } $str = ' '.$str.' '; foreach ($censored as $badword) { if ($replacement != '') { $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); } else { $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); } } return trim($str); } // ------------------------------------------------------------------------ /** * Code Highlighter * * Colorizes code strings * * @access public * @param string the text string * @return string */ function highlight_code($str) { // The highlight string function encodes and highlights // brackets so we need them to start raw $str = str_replace(array('<', '>'), array('<', '>'), $str); // Replace any existing PHP tags to temporary markers so they don't accidentally // break the string out of PHP, and thus, thwart the highlighting. $str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str); // The highlight_string function requires that the text be surrounded // by PHP tags. Since we don't know if A) the submitted text has PHP tags, // or B) whether the PHP tags enclose the entire string, we will add our // own PHP tags around the string along with some markers to make replacement easier later $str = ''; // All the magic happens here, baby! $str = highlight_string($str, TRUE); // Prior to PHP 5, the highlight function used icky font tags // so we'll replace them with span tags. if (abs(phpversion()) < 5) { $str = str_replace(array(''), array(''), $str); $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); } // Remove our artificially added PHP $str = preg_replace("#\.+?//tempstart\
\#is", "\n", $str); $str = preg_replace("#\.+?//tempstart\
#is", "\n", $str); $str = preg_replace("#//tempend.+#is", "
\n", $str); // Replace our markers back to PHP tags. $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); //', $tag_close = '') { if ($str == '') { return ''; } if ($phrase != '') { return preg_replace('/('.preg_quote($phrase).')/i', $tag_open."\\1".$tag_close, $str); } return $str; } // ------------------------------------------------------------------------ /** * Word Wrap * * Wraps text at the specified character. Maintains the integrity of words. * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor * will URLs. * * @access public * @param string the text string * @param integer the number of characters to wrap at * @return string */ function word_wrap($str, $charlim = '76') { // Se the character limit if ( ! is_numeric($charlim)) $charlim = 76; // Reduce multiple spaces $str = preg_replace("| +|", " ", $str); // Standardize newlines $str = preg_replace("/\r\n|\r/", "\n", $str); // If the current word is surrounded by {unwrap} tags we'll // strip the entire chunk and replace it with a marker. $unwrap = array(); if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) { for ($i = 0; $i < count($matches['0']); $i++) { $unwrap[] = $matches['1'][$i]; $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); } } // Use PHP's native function to do the initial wordwrap. // We set the cut flag to FALSE so that any individual words that are // too long get left alone. In the next step we'll deal with them. $str = wordwrap($str, $charlim, "\n", FALSE); // Split the string into individual lines of text and cycle through them $output = ""; foreach (explode("\n", $str) as $line) { // Is the line within the allowed character count? // If so we'll join it to the output and continue if (strlen($line) <= $charlim) { $output .= $line."\n"; continue; } $temp = ''; while((strlen($line)) > $charlim) { // If the over-length word is a URL we won't wrap it if (preg_match("!\[url.+\]|://|wwww.!", $line)) { break; } // Trim the word down $temp .= substr($line, 0, $charlim-1); $line = substr($line, $charlim-1); } // If $temp contains data it means we had to split up an over-length // word into smaller chunks so we'll add it back to our current line if ($temp != '') { $output .= $temp . "\n" . $line; } else { $output .= $line; } $output .= "\n"; } // Put our markers back if (count($unwrap) > 0) { foreach ($unwrap as $key => $val) { $output = str_replace("{{unwrapped".$key."}}", $val, $output); } } // Remove the unwrap tags $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); return $output; } ?> \ No newline at end of file += $n) { return trim($out).$end_char; } } } // ------------------------------------------------------------------------ /** * High ASCII to Entities * * Converts High ascii text and MS Word special characters to character entities * * @access public * @param string * @return string */ function ascii_to_entities($str) { $count = 1; $out = ''; $temp = array(); for ($i = 0, $s = strlen($str); $i < $s; $i++) { $ordinal = ord($str[$i]); if ($ordinal < 128) { $out .= $str[$i]; } else { if (count($temp) == 0) { $count = ($ordinal < 224) ? 2 : 3; } $temp[] = $ordinal; if (count($temp) == $count) { $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); $out .= '&#'.$number.';'; $count = 1; $temp = array(); } } } return $out; } // ------------------------------------------------------------------------ /** * Entities to ASCII * * Converts character entities back to ASCII * * @access public * @param string * @param bool * @return string */ function entities_to_ascii($str, $all = TRUE) { if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) { for ($i = 0, $s = count($matches['0']); $i < $s; $i++) { $digits = $matches['1'][$i]; $out = ''; if ($digits < 128) { $out .= chr($digits); } elseif ($digits < 2048) { $out .= chr(192 + (($digits - ($digits % 64)) / 64)); $out .= chr(128 + ($digits % 64)); } else { $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); $out .= chr(128 + ($digits % 64)); } $str = str_replace($matches['0'][$i], $out, $str); } } if ($all) { $str = str_replace(array("&", "<", ">", """, "'", "-"), array("&","<",">","\"", "'", "-"), $str); } return $str; } // ------------------------------------------------------------------------ /** * Word Censoring Function * * Supply a string and an array of disallowed words and any * matched words will be converted to #### or to the replacement * word you've submitted. * * @access public * @param string the text string * @param string the array of censoered words * @param string the optional replacement value * @return string */ function word_censor($str, $censored, $replacement = '') { if ( ! is_array($censored)) { return $str; } $str = ' '.$str.' '; foreach ($censored as $badword) { if ($replacement != '') { $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); } else { $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); } } return trim($str); } // ------------------------------------------------------------------------ /** * Code Highlighter * * Colorizes code strings * * @access public * @param string the text string * @return string */ function highlight_code($str) { // The highlight string function encodes and highlights // brackets so we need them to start raw $str = str_replace(array('<', '>'), array('<', '>'), $str); // Replace any existing PHP tags to temporary markers so they don't accidentally // break the string out of PHP, and thus, thwart the highlighting. $str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str); // The highlight_string function requires that the text be surrounded // by PHP tags. Since we don't know if A) the submitted text has PHP tags, // or B) whether the PHP tags enclose the entire string, we will add our // own PHP tags around the string along with some markers to make replacement easier later $str = ''; // All the magic happens here, baby! $str = highlight_string($str, TRUE); // Prior to PHP 5, the highlight function used icky font tags // so we'll replace them with span tags. if (abs(phpversion()) < 5) { $str = str_replace(array(''), array(''), $str); $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); } // Remove our artificially added PHP $str = preg_replace("#\.+?//tempstart\
\#is", "\n", $str); $str = preg_replace("#\.+?//tempstart\
#is", "\n", $str); $str = preg_replace("#//tempend.+#is", "
\n", $str); // Replace our markers back to PHP tags. $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); //', $tag_close = '') { if ($str == '') { return ''; } if ($phrase != '') { return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str); } return $str; } // ------------------------------------------------------------------------ /** * Word Wrap * * Wraps text at the specified character. Maintains the integrity of words. * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor * will URLs. * * @access public * @param string the text string * @param integer the number of characters to wrap at * @return string */ function word_wrap($str, $charlim = '76') { // Se the character limit if ( ! is_numeric($charlim)) $charlim = 76; // Reduce multiple spaces $str = preg_replace("| +|", " ", $str); // Standardize newlines $str = preg_replace("/\r\n|\r/", "\n", $str); // If the current word is surrounded by {unwrap} tags we'll // strip the entire chunk and replace it with a marker. $unwrap = array(); if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) { for ($i = 0; $i < count($matches['0']); $i++) { $unwrap[] = $matches['1'][$i]; $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); } } // Use PHP's native function to do the initial wordwrap. // We set the cut flag to FALSE so that any individual words that are // too long get left alone. In the next step we'll deal with them. $str = wordwrap($str, $charlim, "\n", FALSE); // Split the string into individual lines of text and cycle through them $output = ""; foreach (explode("\n", $str) as $line) { // Is the line within the allowed character count? // If so we'll join it to the output and continue if (strlen($line) <= $charlim) { $output .= $line."\n"; continue; } $temp = ''; while((strlen($line)) > $charlim) { // If the over-length word is a URL we won't wrap it if (preg_match("!\[url.+\]|://|wwww.!", $line)) { break; } // Trim the word down $temp .= substr($line, 0, $charlim-1); $line = substr($line, $charlim-1); } // If $temp contains data it means we had to split up an over-length // word into smaller chunks so we'll add it back to our current line if ($temp != '') { $output .= $temp . "\n" . $line; } else { $output .= $line; } $output .= "\n"; } // Put our markers back if (count($unwrap) > 0) { foreach ($unwrap as $key => $val) { $output = str_replace("{{unwrapped".$key."}}", $val, $output); } } // Remove the unwrap tags $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); return $output; } ?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 3d879d529107c0c9d3f1e6b894d9ed17b6e6c54f Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Fri, 18 Jan 2008 19:41:32 +0000 Subject: ExpressionEngine Dev Team in credit --- system/helpers/text_helper.php | 422 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 421 insertions(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index b75cd8f78..211ef1055 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -1 +1,421 @@ -= $n) { return trim($out).$end_char; } } } // ------------------------------------------------------------------------ /** * High ASCII to Entities * * Converts High ascii text and MS Word special characters to character entities * * @access public * @param string * @return string */ function ascii_to_entities($str) { $count = 1; $out = ''; $temp = array(); for ($i = 0, $s = strlen($str); $i < $s; $i++) { $ordinal = ord($str[$i]); if ($ordinal < 128) { $out .= $str[$i]; } else { if (count($temp) == 0) { $count = ($ordinal < 224) ? 2 : 3; } $temp[] = $ordinal; if (count($temp) == $count) { $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); $out .= '&#'.$number.';'; $count = 1; $temp = array(); } } } return $out; } // ------------------------------------------------------------------------ /** * Entities to ASCII * * Converts character entities back to ASCII * * @access public * @param string * @param bool * @return string */ function entities_to_ascii($str, $all = TRUE) { if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) { for ($i = 0, $s = count($matches['0']); $i < $s; $i++) { $digits = $matches['1'][$i]; $out = ''; if ($digits < 128) { $out .= chr($digits); } elseif ($digits < 2048) { $out .= chr(192 + (($digits - ($digits % 64)) / 64)); $out .= chr(128 + ($digits % 64)); } else { $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); $out .= chr(128 + ($digits % 64)); } $str = str_replace($matches['0'][$i], $out, $str); } } if ($all) { $str = str_replace(array("&", "<", ">", """, "'", "-"), array("&","<",">","\"", "'", "-"), $str); } return $str; } // ------------------------------------------------------------------------ /** * Word Censoring Function * * Supply a string and an array of disallowed words and any * matched words will be converted to #### or to the replacement * word you've submitted. * * @access public * @param string the text string * @param string the array of censoered words * @param string the optional replacement value * @return string */ function word_censor($str, $censored, $replacement = '') { if ( ! is_array($censored)) { return $str; } $str = ' '.$str.' '; foreach ($censored as $badword) { if ($replacement != '') { $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); } else { $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); } } return trim($str); } // ------------------------------------------------------------------------ /** * Code Highlighter * * Colorizes code strings * * @access public * @param string the text string * @return string */ function highlight_code($str) { // The highlight string function encodes and highlights // brackets so we need them to start raw $str = str_replace(array('<', '>'), array('<', '>'), $str); // Replace any existing PHP tags to temporary markers so they don't accidentally // break the string out of PHP, and thus, thwart the highlighting. $str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str); // The highlight_string function requires that the text be surrounded // by PHP tags. Since we don't know if A) the submitted text has PHP tags, // or B) whether the PHP tags enclose the entire string, we will add our // own PHP tags around the string along with some markers to make replacement easier later $str = ''; // All the magic happens here, baby! $str = highlight_string($str, TRUE); // Prior to PHP 5, the highlight function used icky font tags // so we'll replace them with span tags. if (abs(phpversion()) < 5) { $str = str_replace(array(''), array(''), $str); $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); } // Remove our artificially added PHP $str = preg_replace("#\.+?//tempstart\
\#is", "\n", $str); $str = preg_replace("#\.+?//tempstart\
#is", "\n", $str); $str = preg_replace("#//tempend.+#is", "
\n", $str); // Replace our markers back to PHP tags. $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); //', $tag_close = '') { if ($str == '') { return ''; } if ($phrase != '') { return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str); } return $str; } // ------------------------------------------------------------------------ /** * Word Wrap * * Wraps text at the specified character. Maintains the integrity of words. * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor * will URLs. * * @access public * @param string the text string * @param integer the number of characters to wrap at * @return string */ function word_wrap($str, $charlim = '76') { // Se the character limit if ( ! is_numeric($charlim)) $charlim = 76; // Reduce multiple spaces $str = preg_replace("| +|", " ", $str); // Standardize newlines $str = preg_replace("/\r\n|\r/", "\n", $str); // If the current word is surrounded by {unwrap} tags we'll // strip the entire chunk and replace it with a marker. $unwrap = array(); if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) { for ($i = 0; $i < count($matches['0']); $i++) { $unwrap[] = $matches['1'][$i]; $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); } } // Use PHP's native function to do the initial wordwrap. // We set the cut flag to FALSE so that any individual words that are // too long get left alone. In the next step we'll deal with them. $str = wordwrap($str, $charlim, "\n", FALSE); // Split the string into individual lines of text and cycle through them $output = ""; foreach (explode("\n", $str) as $line) { // Is the line within the allowed character count? // If so we'll join it to the output and continue if (strlen($line) <= $charlim) { $output .= $line."\n"; continue; } $temp = ''; while((strlen($line)) > $charlim) { // If the over-length word is a URL we won't wrap it if (preg_match("!\[url.+\]|://|wwww.!", $line)) { break; } // Trim the word down $temp .= substr($line, 0, $charlim-1); $line = substr($line, $charlim-1); } // If $temp contains data it means we had to split up an over-length // word into smaller chunks so we'll add it back to our current line if ($temp != '') { $output .= $temp . "\n" . $line; } else { $output .= $line; } $output .= "\n"; } // Put our markers back if (count($unwrap) > 0) { foreach ($unwrap as $key => $val) { $output = str_replace("{{unwrapped".$key."}}", $val, $output); } } // Remove the unwrap tags $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); return $output; } ?> \ No newline at end of file += $n) + { + return trim($out).$end_char; + } + } +} + +// ------------------------------------------------------------------------ + +/** + * High ASCII to Entities + * + * Converts High ascii text and MS Word special characters to character entities + * + * @access public + * @param string + * @return string + */ +function ascii_to_entities($str) +{ + $count = 1; + $out = ''; + $temp = array(); + + for ($i = 0, $s = strlen($str); $i < $s; $i++) + { + $ordinal = ord($str[$i]); + + if ($ordinal < 128) + { + $out .= $str[$i]; + } + else + { + if (count($temp) == 0) + { + $count = ($ordinal < 224) ? 2 : 3; + } + + $temp[] = $ordinal; + + if (count($temp) == $count) + { + $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); + + $out .= '&#'.$number.';'; + $count = 1; + $temp = array(); + } + } + } + + return $out; +} + +// ------------------------------------------------------------------------ + +/** + * Entities to ASCII + * + * Converts character entities back to ASCII + * + * @access public + * @param string + * @param bool + * @return string + */ +function entities_to_ascii($str, $all = TRUE) +{ + if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) + { + for ($i = 0, $s = count($matches['0']); $i < $s; $i++) + { + $digits = $matches['1'][$i]; + + $out = ''; + + if ($digits < 128) + { + $out .= chr($digits); + + } + elseif ($digits < 2048) + { + $out .= chr(192 + (($digits - ($digits % 64)) / 64)); + $out .= chr(128 + ($digits % 64)); + } + else + { + $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); + $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); + $out .= chr(128 + ($digits % 64)); + } + + $str = str_replace($matches['0'][$i], $out, $str); + } + } + + if ($all) + { + $str = str_replace(array("&", "<", ">", """, "'", "-"), + array("&","<",">","\"", "'", "-"), + $str); + } + + return $str; +} + +// ------------------------------------------------------------------------ + +/** + * Word Censoring Function + * + * Supply a string and an array of disallowed words and any + * matched words will be converted to #### or to the replacement + * word you've submitted. + * + * @access public + * @param string the text string + * @param string the array of censoered words + * @param string the optional replacement value + * @return string + */ +function word_censor($str, $censored, $replacement = '') +{ + if ( ! is_array($censored)) + { + return $str; + } + + $str = ' '.$str.' '; + foreach ($censored as $badword) + { + if ($replacement != '') + { + $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); + } + else + { + $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); + } + } + + return trim($str); +} + +// ------------------------------------------------------------------------ + +/** + * Code Highlighter + * + * Colorizes code strings + * + * @access public + * @param string the text string + * @return string + */ +function highlight_code($str) +{ + // The highlight string function encodes and highlights + // brackets so we need them to start raw + $str = str_replace(array('<', '>'), array('<', '>'), $str); + + // Replace any existing PHP tags to temporary markers so they don't accidentally + // break the string out of PHP, and thus, thwart the highlighting. + + $str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str); + + // The highlight_string function requires that the text be surrounded + // by PHP tags. Since we don't know if A) the submitted text has PHP tags, + // or B) whether the PHP tags enclose the entire string, we will add our + // own PHP tags around the string along with some markers to make replacement easier later + + $str = ''; + + // All the magic happens here, baby! + $str = highlight_string($str, TRUE); + + // Prior to PHP 5, the highlight function used icky font tags + // so we'll replace them with span tags. + if (abs(phpversion()) < 5) + { + $str = str_replace(array(''), array(''), $str); + $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); + } + + // Remove our artificially added PHP + $str = preg_replace("#\.+?//tempstart\
\#is", "\n", $str); + $str = preg_replace("#\.+?//tempstart\
#is", "\n", $str); + $str = preg_replace("#//tempend.+#is", "
\n", $str); + + // Replace our markers back to PHP tags. + $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); //', $tag_close = '') +{ + if ($str == '') + { + return ''; + } + + if ($phrase != '') + { + return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str); + } + + return $str; +} + +// ------------------------------------------------------------------------ + +/** + * Word Wrap + * + * Wraps text at the specified character. Maintains the integrity of words. + * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor + * will URLs. + * + * @access public + * @param string the text string + * @param integer the number of characters to wrap at + * @return string + */ +function word_wrap($str, $charlim = '76') +{ + // Se the character limit + if ( ! is_numeric($charlim)) + $charlim = 76; + + // Reduce multiple spaces + $str = preg_replace("| +|", " ", $str); + + // Standardize newlines + $str = preg_replace("/\r\n|\r/", "\n", $str); + + // If the current word is surrounded by {unwrap} tags we'll + // strip the entire chunk and replace it with a marker. + $unwrap = array(); + if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) + { + for ($i = 0; $i < count($matches['0']); $i++) + { + $unwrap[] = $matches['1'][$i]; + $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); + } + } + + // Use PHP's native function to do the initial wordwrap. + // We set the cut flag to FALSE so that any individual words that are + // too long get left alone. In the next step we'll deal with them. + $str = wordwrap($str, $charlim, "\n", FALSE); + + // Split the string into individual lines of text and cycle through them + $output = ""; + foreach (explode("\n", $str) as $line) + { + // Is the line within the allowed character count? + // If so we'll join it to the output and continue + if (strlen($line) <= $charlim) + { + $output .= $line."\n"; + continue; + } + + $temp = ''; + while((strlen($line)) > $charlim) + { + // If the over-length word is a URL we won't wrap it + if (preg_match("!\[url.+\]|://|wwww.!", $line)) + { + break; + } + + // Trim the word down + $temp .= substr($line, 0, $charlim-1); + $line = substr($line, $charlim-1); + } + + // If $temp contains data it means we had to split up an over-length + // word into smaller chunks so we'll add it back to our current line + if ($temp != '') + { + $output .= $temp . "\n" . $line; + } + else + { + $output .= $line; + } + + $output .= "\n"; + } + + // Put our markers back + if (count($unwrap) > 0) + { + foreach ($unwrap as $key => $val) + { + $output = str_replace("{{unwrapped".$key."}}", $val, $output); + } + } + + // Remove the unwrap tags + $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); + + return $output; +} + + +?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 7a9193afa6d890a91eb3528fa0e62df799b07ed6 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Mon, 21 Jan 2008 18:39:20 +0000 Subject: replaced www.codeigniter.com with codeigniter.com --- system/helpers/text_helper.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 211ef1055..9620e03e1 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -7,8 +7,8 @@ * @package CodeIgniter * @author ExpressionEngine Dev Team * @copyright Copyright (c) 2006, EllisLab, Inc. - * @license http://www.codeigniter.com/user_guide/license.html - * @link http://www.codeigniter.com + * @license http://codeigniter.com/user_guide/license.html + * @link http://codeigniter.com * @since Version 1.0 * @filesource */ @@ -22,7 +22,7 @@ * @subpackage Helpers * @category Helpers * @author ExpressionEngine Dev Team - * @link http://www.codeigniter.com/user_guide/helpers/text_helper.html + * @link http://codeigniter.com/user_guide/helpers/text_helper.html */ // ------------------------------------------------------------------------ -- cgit v1.2.3-24-g4f1b From 9468f3ebe207f73aa5871b5eeca26184dbccbfd1 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Tue, 22 Jan 2008 19:19:27 +0000 Subject: fixed bug #3156 in highlight_code() where PHP tags were not being converted properly. Also added protection for asp and inline style script delimiters, and removed an empty span to make outputted code valid --- system/helpers/text_helper.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 9620e03e1..8b671140d 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -259,8 +259,9 @@ function highlight_code($str) // Replace any existing PHP tags to temporary markers so they don't accidentally // break the string out of PHP, and thus, thwart the highlighting. - $str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str); - + $str = str_replace(array('', '<%', '%>', '\\', ''), + array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str); + // The highlight_string function requires that the text be surrounded // by PHP tags. Since we don't know if A) the submitted text has PHP tags, // or B) whether the PHP tags enclose the entire string, we will add our @@ -279,14 +280,16 @@ function highlight_code($str) $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); } - // Remove our artificially added PHP + // Remove our artificially added PHP and the empty span that results from our temp markers $str = preg_replace("#\.+?//tempstart\
\#is", "\n", $str); $str = preg_replace("#\.+?//tempstart\
#is", "\n", $str); $str = preg_replace("#//tempend.+#is", "\n", $str); + $str = preg_replace("#\\n
#is", "\n
", $str); // Replace our markers back to PHP tags. - $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); // Date: Tue, 22 Jan 2008 19:51:03 +0000 Subject: modified markers in highlight_code() to not introduce unintended highlighting, and to allow code where the first line is a code comment to be styled properly --- system/helpers/text_helper.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 8b671140d..2a42cba7e 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -267,7 +267,7 @@ function highlight_code($str) // or B) whether the PHP tags enclose the entire string, we will add our // own PHP tags around the string along with some markers to make replacement easier later - $str = ''; + $str = ''; // All the magic happens here, baby! $str = highlight_string($str, TRUE); @@ -281,10 +281,9 @@ function highlight_code($str) } // Remove our artificially added PHP and the empty span that results from our temp markers - $str = preg_replace("#\.+?//tempstart\
\#is", "\n", $str); - $str = preg_replace("#\.+?//tempstart\
#is", "\n", $str); - $str = preg_replace("#//tempend.+#is", "\n", $str); - $str = preg_replace("#\\n
#is", "\n", $str); + $str = preg_replace("#\.+?tempstart\
\#is", "\n", $str); + $str = preg_replace("#\.+?tempstart\
#is", "\n", $str); + $str = preg_replace("#tempend.+#is", "\n", $str); // Replace our markers back to PHP tags. $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), -- cgit v1.2.3-24-g4f1b From 9cee79136d2b197584bc6e076e7df5d69344be1c Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Tue, 22 Jan 2008 20:12:47 +0000 Subject: further refinement to highlight_code()'s regex replacements --- system/helpers/text_helper.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 2a42cba7e..20bf32335 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -280,9 +280,8 @@ function highlight_code($str) $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); } - // Remove our artificially added PHP and the empty span that results from our temp markers - $str = preg_replace("#\.+?tempstart\
\#is", "\n", $str); - $str = preg_replace("#\.+?tempstart\
#is", "\n", $str); + // Remove our artificially added PHP + $str = preg_replace("#\.+?tempstart\
(?:\)?#is", "\n", $str); $str = preg_replace("#tempend.+#is", "\n", $str); // Replace our markers back to PHP tags. -- cgit v1.2.3-24-g4f1b From 269b942a2bf7b022795e591d9b0ad04526ee7e09 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Mon, 28 Jan 2008 21:00:20 +0000 Subject: added ability to "extend" helpers * modified Loader to check for prefixed helpers in application/helpers folder * surrounded provided helper functions with if (! function_exists('foo')) conditionals so the user's helper functions take precedent. --- system/helpers/text_helper.php | 431 ++++++++++++++++++++++------------------- 1 file changed, 227 insertions(+), 204 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 20bf32335..30cec3148 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -38,27 +38,30 @@ * @param string the end character. Usually an ellipsis * @return string */ -function word_limiter($str, $n = 100, $end_char = '…') +if (! function_exists('word_limiter')) { - if (strlen($str) < $n) + function word_limiter($str, $n = 100, $end_char = '…') { - return $str; - } + if (strlen($str) < $n) + { + return $str; + } - $words = explode(' ', preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", " ", $str))); + $words = explode(' ', preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", " ", $str))); - if (count($words) <= $n) - { - return $str; - } + if (count($words) <= $n) + { + return $str; + } - $str = ''; - for ($i = 0; $i < $n; $i++) - { - $str .= $words[$i].' '; - } + $str = ''; + for ($i = 0; $i < $n; $i++) + { + $str .= $words[$i].' '; + } - return trim($str).$end_char; + return trim($str).$end_char; + } } // ------------------------------------------------------------------------ @@ -75,28 +78,31 @@ function word_limiter($str, $n = 100, $end_char = '…') * @param string the end character. Usually an ellipsis * @return string */ -function character_limiter($str, $n = 500, $end_char = '…') +if (! function_exists('character_limiter')) { - if (strlen($str) < $n) + function character_limiter($str, $n = 500, $end_char = '…') { - return $str; - } + if (strlen($str) < $n) + { + return $str; + } - $str = preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", " ", $str)); + $str = preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", " ", $str)); - if (strlen($str) <= $n) - { - return $str; - } + if (strlen($str) <= $n) + { + return $str; + } - $out = ""; - foreach (explode(' ', trim($str)) as $val) - { - $out .= $val.' '; - if (strlen($out) >= $n) + $out = ""; + foreach (explode(' ', trim($str)) as $val) { - return trim($out).$end_char; - } + $out .= $val.' '; + if (strlen($out) >= $n) + { + return trim($out).$end_char; + } + } } } @@ -111,41 +117,44 @@ function character_limiter($str, $n = 500, $end_char = '…') * @param string * @return string */ -function ascii_to_entities($str) +if (! function_exists('ascii_to_entities')) { - $count = 1; - $out = ''; - $temp = array(); - - for ($i = 0, $s = strlen($str); $i < $s; $i++) - { - $ordinal = ord($str[$i]); + function ascii_to_entities($str) + { + $count = 1; + $out = ''; + $temp = array(); - if ($ordinal < 128) - { - $out .= $str[$i]; - } - else + for ($i = 0, $s = strlen($str); $i < $s; $i++) { - if (count($temp) == 0) + $ordinal = ord($str[$i]); + + if ($ordinal < 128) { - $count = ($ordinal < 224) ? 2 : 3; + $out .= $str[$i]; } + else + { + if (count($temp) == 0) + { + $count = ($ordinal < 224) ? 2 : 3; + } - $temp[] = $ordinal; + $temp[] = $ordinal; - if (count($temp) == $count) - { - $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); + if (count($temp) == $count) + { + $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); - $out .= '&#'.$number.';'; - $count = 1; - $temp = array(); + $out .= '&#'.$number.';'; + $count = 1; + $temp = array(); + } } } - } - return $out; + return $out; + } } // ------------------------------------------------------------------------ @@ -160,45 +169,48 @@ function ascii_to_entities($str) * @param bool * @return string */ -function entities_to_ascii($str, $all = TRUE) +if (! function_exists('entities_to_ascii')) { - if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) - { - for ($i = 0, $s = count($matches['0']); $i < $s; $i++) - { - $digits = $matches['1'][$i]; + function entities_to_ascii($str, $all = TRUE) + { + if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) + { + for ($i = 0, $s = count($matches['0']); $i < $s; $i++) + { + $digits = $matches['1'][$i]; - $out = ''; + $out = ''; - if ($digits < 128) - { - $out .= chr($digits); + if ($digits < 128) + { + $out .= chr($digits); - } - elseif ($digits < 2048) - { - $out .= chr(192 + (($digits - ($digits % 64)) / 64)); - $out .= chr(128 + ($digits % 64)); - } - else - { - $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); - $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); - $out .= chr(128 + ($digits % 64)); - } + } + elseif ($digits < 2048) + { + $out .= chr(192 + (($digits - ($digits % 64)) / 64)); + $out .= chr(128 + ($digits % 64)); + } + else + { + $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); + $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); + $out .= chr(128 + ($digits % 64)); + } - $str = str_replace($matches['0'][$i], $out, $str); + $str = str_replace($matches['0'][$i], $out, $str); + } } - } - if ($all) - { - $str = str_replace(array("&", "<", ">", """, "'", "-"), - array("&","<",">","\"", "'", "-"), - $str); - } + if ($all) + { + $str = str_replace(array("&", "<", ">", """, "'", "-"), + array("&","<",">","\"", "'", "-"), + $str); + } - return $str; + return $str; + } } // ------------------------------------------------------------------------ @@ -216,27 +228,30 @@ function entities_to_ascii($str, $all = TRUE) * @param string the optional replacement value * @return string */ -function word_censor($str, $censored, $replacement = '') +if (! function_exists('word_censor')) { - if ( ! is_array($censored)) - { - return $str; - } - - $str = ' '.$str.' '; - foreach ($censored as $badword) + function word_censor($str, $censored, $replacement = '') { - if ($replacement != '') + if ( ! is_array($censored)) { - $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); + return $str; } - else + + $str = ' '.$str.' '; + foreach ($censored as $badword) { - $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); + if ($replacement != '') + { + $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); + } + else + { + $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); + } } - } - return trim($str); + return trim($str); + } } // ------------------------------------------------------------------------ @@ -250,45 +265,48 @@ function word_censor($str, $censored, $replacement = '') * @param string the text string * @return string */ -function highlight_code($str) -{ - // The highlight string function encodes and highlights - // brackets so we need them to start raw - $str = str_replace(array('<', '>'), array('<', '>'), $str); +if (! function_exists('highlight_code')) +{ + function highlight_code($str) + { + // The highlight string function encodes and highlights + // brackets so we need them to start raw + $str = str_replace(array('<', '>'), array('<', '>'), $str); - // Replace any existing PHP tags to temporary markers so they don't accidentally - // break the string out of PHP, and thus, thwart the highlighting. + // Replace any existing PHP tags to temporary markers so they don't accidentally + // break the string out of PHP, and thus, thwart the highlighting. - $str = str_replace(array('', '<%', '%>', '\\', ''), - array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str); + $str = str_replace(array('', '<%', '%>', '\\', ''), + array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str); - // The highlight_string function requires that the text be surrounded - // by PHP tags. Since we don't know if A) the submitted text has PHP tags, - // or B) whether the PHP tags enclose the entire string, we will add our - // own PHP tags around the string along with some markers to make replacement easier later + // The highlight_string function requires that the text be surrounded + // by PHP tags. Since we don't know if A) the submitted text has PHP tags, + // or B) whether the PHP tags enclose the entire string, we will add our + // own PHP tags around the string along with some markers to make replacement easier later - $str = ''; + $str = ''; - // All the magic happens here, baby! - $str = highlight_string($str, TRUE); + // All the magic happens here, baby! + $str = highlight_string($str, TRUE); - // Prior to PHP 5, the highlight function used icky font tags - // so we'll replace them with span tags. - if (abs(phpversion()) < 5) - { - $str = str_replace(array(''), array(''), $str); - $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); - } + // Prior to PHP 5, the highlight function used icky font tags + // so we'll replace them with span tags. + if (abs(phpversion()) < 5) + { + $str = str_replace(array(''), array(''), $str); + $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); + } - // Remove our artificially added PHP - $str = preg_replace("#\.+?tempstart\
(?:\)?#is", "\n", $str); - $str = preg_replace("#tempend.+#is", "
\n", $str); + // Remove our artificially added PHP + $str = preg_replace("#\.+?tempstart\
(?:\)?#is", "\n", $str); + $str = preg_replace("#tempend.+#is", "
\n", $str); - // Replace our markers back to PHP tags. - $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), - array('<?', '?>', '<%', '%>', '\\', '</script>'), $str); + // Replace our markers back to PHP tags. + $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), + array('<?', '?>', '<%', '%>', '\\', '</script>'), $str); - return $str; + return $str; + } } // ------------------------------------------------------------------------ @@ -305,19 +323,22 @@ function highlight_code($str) * @param string the closing tag to end the phrase with * @return string */ -function highlight_phrase($str, $phrase, $tag_open = '', $tag_close = '') +if (! function_exists('highlight_phrase')) { - if ($str == '') + function highlight_phrase($str, $phrase, $tag_open = '', $tag_close = '') { - return ''; - } + if ($str == '') + { + return ''; + } - if ($phrase != '') - { - return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str); - } + if ($phrase != '') + { + return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str); + } - return $str; + return $str; + } } // ------------------------------------------------------------------------ @@ -334,89 +355,91 @@ function highlight_phrase($str, $phrase, $tag_open = '', $tag_close = '< * @param integer the number of characters to wrap at * @return string */ -function word_wrap($str, $charlim = '76') +if (! function_exists('word_wrap')) { - // Se the character limit - if ( ! is_numeric($charlim)) - $charlim = 76; + function word_wrap($str, $charlim = '76') + { + // Se the character limit + if ( ! is_numeric($charlim)) + $charlim = 76; - // Reduce multiple spaces - $str = preg_replace("| +|", " ", $str); + // Reduce multiple spaces + $str = preg_replace("| +|", " ", $str); - // Standardize newlines - $str = preg_replace("/\r\n|\r/", "\n", $str); + // Standardize newlines + $str = preg_replace("/\r\n|\r/", "\n", $str); - // If the current word is surrounded by {unwrap} tags we'll - // strip the entire chunk and replace it with a marker. - $unwrap = array(); - if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) - { - for ($i = 0; $i < count($matches['0']); $i++) + // If the current word is surrounded by {unwrap} tags we'll + // strip the entire chunk and replace it with a marker. + $unwrap = array(); + if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) { - $unwrap[] = $matches['1'][$i]; - $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); + for ($i = 0; $i < count($matches['0']); $i++) + { + $unwrap[] = $matches['1'][$i]; + $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); + } } - } - // Use PHP's native function to do the initial wordwrap. - // We set the cut flag to FALSE so that any individual words that are - // too long get left alone. In the next step we'll deal with them. - $str = wordwrap($str, $charlim, "\n", FALSE); + // Use PHP's native function to do the initial wordwrap. + // We set the cut flag to FALSE so that any individual words that are + // too long get left alone. In the next step we'll deal with them. + $str = wordwrap($str, $charlim, "\n", FALSE); - // Split the string into individual lines of text and cycle through them - $output = ""; - foreach (explode("\n", $str) as $line) - { - // Is the line within the allowed character count? - // If so we'll join it to the output and continue - if (strlen($line) <= $charlim) + // Split the string into individual lines of text and cycle through them + $output = ""; + foreach (explode("\n", $str) as $line) { - $output .= $line."\n"; - continue; - } - - $temp = ''; - while((strlen($line)) > $charlim) - { - // If the over-length word is a URL we won't wrap it - if (preg_match("!\[url.+\]|://|wwww.!", $line)) + // Is the line within the allowed character count? + // If so we'll join it to the output and continue + if (strlen($line) <= $charlim) { - break; + $output .= $line."\n"; + continue; } + + $temp = ''; + while((strlen($line)) > $charlim) + { + // If the over-length word is a URL we won't wrap it + if (preg_match("!\[url.+\]|://|wwww.!", $line)) + { + break; + } - // Trim the word down - $temp .= substr($line, 0, $charlim-1); - $line = substr($line, $charlim-1); - } + // Trim the word down + $temp .= substr($line, 0, $charlim-1); + $line = substr($line, $charlim-1); + } - // If $temp contains data it means we had to split up an over-length - // word into smaller chunks so we'll add it back to our current line - if ($temp != '') - { - $output .= $temp . "\n" . $line; - } - else - { - $output .= $line; - } + // If $temp contains data it means we had to split up an over-length + // word into smaller chunks so we'll add it back to our current line + if ($temp != '') + { + $output .= $temp . "\n" . $line; + } + else + { + $output .= $line; + } - $output .= "\n"; - } + $output .= "\n"; + } - // Put our markers back - if (count($unwrap) > 0) - { - foreach ($unwrap as $key => $val) - { - $output = str_replace("{{unwrapped".$key."}}", $val, $output); + // Put our markers back + if (count($unwrap) > 0) + { + foreach ($unwrap as $key => $val) + { + $output = str_replace("{{unwrapped".$key."}}", $val, $output); + } } - } - // Remove the unwrap tags - $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); + // Remove the unwrap tags + $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); - return $output; + return $output; + } } - ?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 2d87b4d2fa7d2fd01876eaa8cf1f727d863d3e6c Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 30 Jan 2008 15:19:53 +0000 Subject: Fixed a bug (#1872) where word_limiter() was not retaining whitespace. --- system/helpers/text_helper.php | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 30cec3148..21ab77830 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -40,27 +40,21 @@ */ if (! function_exists('word_limiter')) { - function word_limiter($str, $n = 100, $end_char = '…') + function word_limiter($str, $limit = 100, $end_char = '…') { - if (strlen($str) < $n) + if (trim($str) == '') { return $str; } - $words = explode(' ', preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", " ", $str))); - - if (count($words) <= $n) - { - return $str; - } + preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches); - $str = ''; - for ($i = 0; $i < $n; $i++) + if (strlen($str) == strlen($matches[0])) { - $str .= $words[$i].' '; + $end_char = ''; } - - return trim($str).$end_char; + + return rtrim($matches[0]).$end_char; } } -- cgit v1.2.3-24-g4f1b From 7327499064ae165468c7440f8571c3e570b58a0b Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Mon, 5 May 2008 16:39:18 +0000 Subject: Added get_dir_file_info(), get_file_info(), and get_mime_by_extension() to the File Helper. Changed ( ! condition) into (! condition) within the code --- system/helpers/text_helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 21ab77830..df3c89e6f 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -226,7 +226,7 @@ if (! function_exists('word_censor')) { function word_censor($str, $censored, $replacement = '') { - if ( ! is_array($censored)) + if (! is_array($censored)) { return $str; } @@ -354,7 +354,7 @@ if (! function_exists('word_wrap')) function word_wrap($str, $charlim = '76') { // Se the character limit - if ( ! is_numeric($charlim)) + if (! is_numeric($charlim)) $charlim = 76; // Reduce multiple spaces -- cgit v1.2.3-24-g4f1b From 5583e1aae64ff7e902136c4ba610d438dc2015d4 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Sun, 11 May 2008 15:48:20 +0000 Subject: removed closing PHP tag from all framework files --- system/helpers/text_helper.php | 1 - 1 file changed, 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index df3c89e6f..95c2c619b 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -436,4 +436,3 @@ if (! function_exists('word_wrap')) } } -?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b From c7deac9f2f9e43cedb18202542e8a46061df046e Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Sun, 11 May 2008 16:27:41 +0000 Subject: Undoing change committed in r1115 --- system/helpers/text_helper.php | 1 + 1 file changed, 1 insertion(+) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 95c2c619b..df3c89e6f 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -436,3 +436,4 @@ if (! function_exists('word_wrap')) } } +?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b From a3ffbbb75ab9403941e4f810703313432b3993cc Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Sun, 11 May 2008 18:18:29 +0000 Subject: Removed closing PHP tags, replaced with a comment block identifying the end of the file --- system/helpers/text_helper.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index df3c89e6f..2d03699f3 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -436,4 +436,6 @@ if (! function_exists('word_wrap')) } } -?> \ No newline at end of file + +/* End of file text_helper.php */ +/* Location: ./system/helpers/text_helper.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 0b59f270a432f8c7b6128981f0a39b4a2e2fbd34 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Tue, 13 May 2008 04:22:33 +0000 Subject: Some sweeping syntax changes for consistency: (! foo) changed to ( ! foo) || changed to OR changed newline standardization code in various places from preg_replace to str_replace --- system/helpers/text_helper.php | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 2d03699f3..18f33d2b3 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -1,4 +1,4 @@ -', $tag_close = '') { @@ -349,19 +349,22 @@ if (! function_exists('highlight_phrase')) * @param integer the number of characters to wrap at * @return string */ -if (! function_exists('word_wrap')) +if ( ! function_exists('word_wrap')) { function word_wrap($str, $charlim = '76') { // Se the character limit - if (! is_numeric($charlim)) + if ( ! is_numeric($charlim)) $charlim = 76; // Reduce multiple spaces $str = preg_replace("| +|", " ", $str); // Standardize newlines - $str = preg_replace("/\r\n|\r/", "\n", $str); + if (strpos($str, "\r") !== FALSE) + { + $str = str_replace(array("\r\n", "\r"), "\n", $str); + } // If the current word is surrounded by {unwrap} tags we'll // strip the entire chunk and replace it with a marker. @@ -436,6 +439,6 @@ if (! function_exists('word_wrap')) } } - -/* End of file text_helper.php */ + +/* End of file text_helper.php */ /* Location: ./system/helpers/text_helper.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 86d721ca61641289807af421bd79456851cd311d Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Fri, 12 Sep 2008 23:33:40 +0000 Subject: updated copyright --- system/helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 18f33d2b3..954eb797a 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -6,7 +6,7 @@ * * @package CodeIgniter * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2006, EllisLab, Inc. + * @copyright Copyright (c) 2008, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 -- cgit v1.2.3-24-g4f1b From c0e72b0609827484dc822ded7fcd0a312ed4eb98 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Fri, 3 Oct 2008 18:56:06 +0000 Subject: modified how we're performing text highlighting to prevent extra or missing span tags. Namely, removed the start and ending markers, as if the code being highlighted would be styled the same, PHP would put it all in the same , so when we removed our own additions, it would remove the needed opening span style --- system/helpers/text_helper.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 954eb797a..54ef1b580 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -274,27 +274,27 @@ if ( ! function_exists('highlight_code')) array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str); // The highlight_string function requires that the text be surrounded - // by PHP tags. Since we don't know if A) the submitted text has PHP tags, - // or B) whether the PHP tags enclose the entire string, we will add our - // own PHP tags around the string along with some markers to make replacement easier later - - $str = ''; - - // All the magic happens here, baby! + // by PHP tags, which we will remove later + $str = ''; // tags + // so we'll replace them with tags. + + if (abs(PHP_VERSION) < 5) { $str = str_replace(array(''), array(''), $str); $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); + $str = str_replace('<?php ', '', $str); } - - // Remove our artificially added PHP - $str = preg_replace("#\.+?tempstart\
(?:\)?#is", "\n", $str); - $str = preg_replace("#tempend.+#is", "
\n", $str); - + + // Remove our artificially added PHP, and the syntax highlighting that came with it + $str = str_replace('<?php ', '', $str); + $str = preg_replace('/(.*?)\?><\/span>\n<\/span>\n<\/code>/is', "$1\n\n
", $str); + $str = preg_replace('/<\/span>/i', '', $str); + // Replace our markers back to PHP tags. $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), array('<?', '?>', '<%', '%>', '\\', '</script>'), $str); -- cgit v1.2.3-24-g4f1b From 337c74ab2af0dec69659f8c68c82b12c878c1b88 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Fri, 3 Oct 2008 19:09:53 +0000 Subject: tweak of temp tag and highlighting replacement to accommodate environments with different colors specified for highlight_string() --- system/helpers/text_helper.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 54ef1b580..9f13896ae 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -287,12 +287,11 @@ if ( ! function_exists('highlight_code')) { $str = str_replace(array(''), array(''), $str); $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); - $str = str_replace('<?php ', '', $str); } - + // Remove our artificially added PHP, and the syntax highlighting that came with it - $str = str_replace('<?php ', '', $str); - $str = preg_replace('/(.*?)\?><\/span>\n<\/span>\n<\/code>/is', "$1\n\n
", $str); + $str = preg_replace('/<\?php( | )/i', '', $str); + $str = preg_replace('/(.*?)\?><\/span>\n<\/span>\n<\/code>/is', "$1\n\n", $str); $str = preg_replace('/<\/span>/i', '', $str); // Replace our markers back to PHP tags. -- cgit v1.2.3-24-g4f1b From 87da39a77bce2660e77565136de0530b757267cb Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Fri, 17 Oct 2008 08:13:55 +0000 Subject: Fixed a preg_replace bug if the supplied string contains the delimiter --- system/helpers/text_helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 9f13896ae..8a4460825 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -236,11 +236,11 @@ if ( ! function_exists('word_censor')) { if ($replacement != '') { - $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); + $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/i", $replacement, $str); } else { - $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); + $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); } } -- cgit v1.2.3-24-g4f1b From 2067d1a727e7eb5e5ffb40e967f3d1fc4c8a41b2 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Thu, 13 Nov 2008 22:59:24 +0000 Subject: Changing EOL style to LF --- system/helpers/text_helper.php | 884 ++++++++++++++++++++--------------------- 1 file changed, 442 insertions(+), 442 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 8a4460825..6e61f776a 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -1,443 +1,443 @@ -= $n) - { - return trim($out).$end_char; - } - } - } -} - -// ------------------------------------------------------------------------ - -/** - * High ASCII to Entities - * - * Converts High ascii text and MS Word special characters to character entities - * - * @access public - * @param string - * @return string - */ -if ( ! function_exists('ascii_to_entities')) -{ - function ascii_to_entities($str) - { - $count = 1; - $out = ''; - $temp = array(); - - for ($i = 0, $s = strlen($str); $i < $s; $i++) - { - $ordinal = ord($str[$i]); - - if ($ordinal < 128) - { - $out .= $str[$i]; - } - else - { - if (count($temp) == 0) - { - $count = ($ordinal < 224) ? 2 : 3; - } - - $temp[] = $ordinal; - - if (count($temp) == $count) - { - $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); - - $out .= '&#'.$number.';'; - $count = 1; - $temp = array(); - } - } - } - - return $out; - } -} - -// ------------------------------------------------------------------------ - -/** - * Entities to ASCII - * - * Converts character entities back to ASCII - * - * @access public - * @param string - * @param bool - * @return string - */ -if ( ! function_exists('entities_to_ascii')) -{ - function entities_to_ascii($str, $all = TRUE) - { - if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) - { - for ($i = 0, $s = count($matches['0']); $i < $s; $i++) - { - $digits = $matches['1'][$i]; - - $out = ''; - - if ($digits < 128) - { - $out .= chr($digits); - - } - elseif ($digits < 2048) - { - $out .= chr(192 + (($digits - ($digits % 64)) / 64)); - $out .= chr(128 + ($digits % 64)); - } - else - { - $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); - $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); - $out .= chr(128 + ($digits % 64)); - } - - $str = str_replace($matches['0'][$i], $out, $str); - } - } - - if ($all) - { - $str = str_replace(array("&", "<", ">", """, "'", "-"), - array("&","<",">","\"", "'", "-"), - $str); - } - - return $str; - } -} - -// ------------------------------------------------------------------------ - -/** - * Word Censoring Function - * - * Supply a string and an array of disallowed words and any - * matched words will be converted to #### or to the replacement - * word you've submitted. - * - * @access public - * @param string the text string - * @param string the array of censoered words - * @param string the optional replacement value - * @return string - */ -if ( ! function_exists('word_censor')) -{ - function word_censor($str, $censored, $replacement = '') - { - if ( ! is_array($censored)) - { - return $str; - } - - $str = ' '.$str.' '; - foreach ($censored as $badword) - { - if ($replacement != '') - { - $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/i", $replacement, $str); - } - else - { - $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); - } - } - - return trim($str); - } -} - -// ------------------------------------------------------------------------ - -/** - * Code Highlighter - * - * Colorizes code strings - * - * @access public - * @param string the text string - * @return string - */ -if ( ! function_exists('highlight_code')) -{ - function highlight_code($str) - { - // The highlight string function encodes and highlights - // brackets so we need them to start raw - $str = str_replace(array('<', '>'), array('<', '>'), $str); - - // Replace any existing PHP tags to temporary markers so they don't accidentally - // break the string out of PHP, and thus, thwart the highlighting. - - $str = str_replace(array('', '<%', '%>', '\\', ''), - array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str); - - // The highlight_string function requires that the text be surrounded - // by PHP tags, which we will remove later - $str = ''; // tags - // so we'll replace them with tags. - - if (abs(PHP_VERSION) < 5) - { - $str = str_replace(array(''), array(''), $str); - $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); - } - - // Remove our artificially added PHP, and the syntax highlighting that came with it - $str = preg_replace('/<\?php( | )/i', '', $str); - $str = preg_replace('/(.*?)\?><\/span>\n<\/span>\n<\/code>/is', "$1\n\n", $str); - $str = preg_replace('/<\/span>/i', '', $str); - - // Replace our markers back to PHP tags. - $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), - array('<?', '?>', '<%', '%>', '\\', '</script>'), $str); - - return $str; - } -} - -// ------------------------------------------------------------------------ - -/** - * Phrase Highlighter - * - * Highlights a phrase within a text string - * - * @access public - * @param string the text string - * @param string the phrase you'd like to highlight - * @param string the openging tag to precede the phrase with - * @param string the closing tag to end the phrase with - * @return string - */ -if ( ! function_exists('highlight_phrase')) -{ - function highlight_phrase($str, $phrase, $tag_open = '', $tag_close = '') - { - if ($str == '') - { - return ''; - } - - if ($phrase != '') - { - return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str); - } - - return $str; - } -} - -// ------------------------------------------------------------------------ - -/** - * Word Wrap - * - * Wraps text at the specified character. Maintains the integrity of words. - * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor - * will URLs. - * - * @access public - * @param string the text string - * @param integer the number of characters to wrap at - * @return string - */ -if ( ! function_exists('word_wrap')) -{ - function word_wrap($str, $charlim = '76') - { - // Se the character limit - if ( ! is_numeric($charlim)) - $charlim = 76; - - // Reduce multiple spaces - $str = preg_replace("| +|", " ", $str); - - // Standardize newlines - if (strpos($str, "\r") !== FALSE) - { - $str = str_replace(array("\r\n", "\r"), "\n", $str); - } - - // If the current word is surrounded by {unwrap} tags we'll - // strip the entire chunk and replace it with a marker. - $unwrap = array(); - if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) - { - for ($i = 0; $i < count($matches['0']); $i++) - { - $unwrap[] = $matches['1'][$i]; - $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); - } - } - - // Use PHP's native function to do the initial wordwrap. - // We set the cut flag to FALSE so that any individual words that are - // too long get left alone. In the next step we'll deal with them. - $str = wordwrap($str, $charlim, "\n", FALSE); - - // Split the string into individual lines of text and cycle through them - $output = ""; - foreach (explode("\n", $str) as $line) - { - // Is the line within the allowed character count? - // If so we'll join it to the output and continue - if (strlen($line) <= $charlim) - { - $output .= $line."\n"; - continue; - } - - $temp = ''; - while((strlen($line)) > $charlim) - { - // If the over-length word is a URL we won't wrap it - if (preg_match("!\[url.+\]|://|wwww.!", $line)) - { - break; - } - - // Trim the word down - $temp .= substr($line, 0, $charlim-1); - $line = substr($line, $charlim-1); - } - - // If $temp contains data it means we had to split up an over-length - // word into smaller chunks so we'll add it back to our current line - if ($temp != '') - { - $output .= $temp . "\n" . $line; - } - else - { - $output .= $line; - } - - $output .= "\n"; - } - - // Put our markers back - if (count($unwrap) > 0) - { - foreach ($unwrap as $key => $val) - { - $output = str_replace("{{unwrapped".$key."}}", $val, $output); - } - } - - // Remove the unwrap tags - $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); - - return $output; - } -} - - -/* End of file text_helper.php */ += $n) + { + return trim($out).$end_char; + } + } + } +} + +// ------------------------------------------------------------------------ + +/** + * High ASCII to Entities + * + * Converts High ascii text and MS Word special characters to character entities + * + * @access public + * @param string + * @return string + */ +if ( ! function_exists('ascii_to_entities')) +{ + function ascii_to_entities($str) + { + $count = 1; + $out = ''; + $temp = array(); + + for ($i = 0, $s = strlen($str); $i < $s; $i++) + { + $ordinal = ord($str[$i]); + + if ($ordinal < 128) + { + $out .= $str[$i]; + } + else + { + if (count($temp) == 0) + { + $count = ($ordinal < 224) ? 2 : 3; + } + + $temp[] = $ordinal; + + if (count($temp) == $count) + { + $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); + + $out .= '&#'.$number.';'; + $count = 1; + $temp = array(); + } + } + } + + return $out; + } +} + +// ------------------------------------------------------------------------ + +/** + * Entities to ASCII + * + * Converts character entities back to ASCII + * + * @access public + * @param string + * @param bool + * @return string + */ +if ( ! function_exists('entities_to_ascii')) +{ + function entities_to_ascii($str, $all = TRUE) + { + if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) + { + for ($i = 0, $s = count($matches['0']); $i < $s; $i++) + { + $digits = $matches['1'][$i]; + + $out = ''; + + if ($digits < 128) + { + $out .= chr($digits); + + } + elseif ($digits < 2048) + { + $out .= chr(192 + (($digits - ($digits % 64)) / 64)); + $out .= chr(128 + ($digits % 64)); + } + else + { + $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); + $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); + $out .= chr(128 + ($digits % 64)); + } + + $str = str_replace($matches['0'][$i], $out, $str); + } + } + + if ($all) + { + $str = str_replace(array("&", "<", ">", """, "'", "-"), + array("&","<",">","\"", "'", "-"), + $str); + } + + return $str; + } +} + +// ------------------------------------------------------------------------ + +/** + * Word Censoring Function + * + * Supply a string and an array of disallowed words and any + * matched words will be converted to #### or to the replacement + * word you've submitted. + * + * @access public + * @param string the text string + * @param string the array of censoered words + * @param string the optional replacement value + * @return string + */ +if ( ! function_exists('word_censor')) +{ + function word_censor($str, $censored, $replacement = '') + { + if ( ! is_array($censored)) + { + return $str; + } + + $str = ' '.$str.' '; + foreach ($censored as $badword) + { + if ($replacement != '') + { + $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/i", $replacement, $str); + } + else + { + $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); + } + } + + return trim($str); + } +} + +// ------------------------------------------------------------------------ + +/** + * Code Highlighter + * + * Colorizes code strings + * + * @access public + * @param string the text string + * @return string + */ +if ( ! function_exists('highlight_code')) +{ + function highlight_code($str) + { + // The highlight string function encodes and highlights + // brackets so we need them to start raw + $str = str_replace(array('<', '>'), array('<', '>'), $str); + + // Replace any existing PHP tags to temporary markers so they don't accidentally + // break the string out of PHP, and thus, thwart the highlighting. + + $str = str_replace(array('', '<%', '%>', '\\', ''), + array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str); + + // The highlight_string function requires that the text be surrounded + // by PHP tags, which we will remove later + $str = ''; // tags + // so we'll replace them with tags. + + if (abs(PHP_VERSION) < 5) + { + $str = str_replace(array(''), array(''), $str); + $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); + } + + // Remove our artificially added PHP, and the syntax highlighting that came with it + $str = preg_replace('/<\?php( | )/i', '', $str); + $str = preg_replace('/(.*?)\?><\/span>\n<\/span>\n<\/code>/is', "$1\n\n", $str); + $str = preg_replace('/<\/span>/i', '', $str); + + // Replace our markers back to PHP tags. + $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), + array('<?', '?>', '<%', '%>', '\\', '</script>'), $str); + + return $str; + } +} + +// ------------------------------------------------------------------------ + +/** + * Phrase Highlighter + * + * Highlights a phrase within a text string + * + * @access public + * @param string the text string + * @param string the phrase you'd like to highlight + * @param string the openging tag to precede the phrase with + * @param string the closing tag to end the phrase with + * @return string + */ +if ( ! function_exists('highlight_phrase')) +{ + function highlight_phrase($str, $phrase, $tag_open = '', $tag_close = '') + { + if ($str == '') + { + return ''; + } + + if ($phrase != '') + { + return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str); + } + + return $str; + } +} + +// ------------------------------------------------------------------------ + +/** + * Word Wrap + * + * Wraps text at the specified character. Maintains the integrity of words. + * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor + * will URLs. + * + * @access public + * @param string the text string + * @param integer the number of characters to wrap at + * @return string + */ +if ( ! function_exists('word_wrap')) +{ + function word_wrap($str, $charlim = '76') + { + // Se the character limit + if ( ! is_numeric($charlim)) + $charlim = 76; + + // Reduce multiple spaces + $str = preg_replace("| +|", " ", $str); + + // Standardize newlines + if (strpos($str, "\r") !== FALSE) + { + $str = str_replace(array("\r\n", "\r"), "\n", $str); + } + + // If the current word is surrounded by {unwrap} tags we'll + // strip the entire chunk and replace it with a marker. + $unwrap = array(); + if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) + { + for ($i = 0; $i < count($matches['0']); $i++) + { + $unwrap[] = $matches['1'][$i]; + $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); + } + } + + // Use PHP's native function to do the initial wordwrap. + // We set the cut flag to FALSE so that any individual words that are + // too long get left alone. In the next step we'll deal with them. + $str = wordwrap($str, $charlim, "\n", FALSE); + + // Split the string into individual lines of text and cycle through them + $output = ""; + foreach (explode("\n", $str) as $line) + { + // Is the line within the allowed character count? + // If so we'll join it to the output and continue + if (strlen($line) <= $charlim) + { + $output .= $line."\n"; + continue; + } + + $temp = ''; + while((strlen($line)) > $charlim) + { + // If the over-length word is a URL we won't wrap it + if (preg_match("!\[url.+\]|://|wwww.!", $line)) + { + break; + } + + // Trim the word down + $temp .= substr($line, 0, $charlim-1); + $line = substr($line, $charlim-1); + } + + // If $temp contains data it means we had to split up an over-length + // word into smaller chunks so we'll add it back to our current line + if ($temp != '') + { + $output .= $temp . "\n" . $line; + } + else + { + $output .= $line; + } + + $output .= "\n"; + } + + // Put our markers back + if (count($unwrap) > 0) + { + foreach ($unwrap as $key => $val) + { + $output = str_replace("{{unwrapped".$key."}}", $val, $output); + } + } + + // Remove the unwrap tags + $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); + + return $output; + } +} + + +/* End of file text_helper.php */ /* Location: ./system/helpers/text_helper.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From f1b721a3559e8eb95bc580a3f79c5c2e896c9932 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 21 Jan 2009 17:52:13 +0000 Subject: Fixed a bug affecting some locales where word censoring would not work on words beginning or ending with an accented character. --- system/helpers/text_helper.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 6e61f776a..e79a2419d 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -230,21 +230,28 @@ if ( ! function_exists('word_censor')) { return $str; } + + $str = ' '.$str.' '; + + // \w, \b and a few others do not match on a unicode character + // set for performance reasons. As a result words like über + // will not match on a word boundary. Instead, we'll assume that + // a bad word will be bookeneded by any of these characters. + $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]'; - $str = ' '.$str.' '; foreach ($censored as $badword) { if ($replacement != '') { - $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/i", $replacement, $str); + $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/i", "\\1{$replacement}\\3", $str); } else { - $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); + $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'", $str); } } - - return trim($str); + + return trim($str); } } -- cgit v1.2.3-24-g4f1b From 01d6b4f663588e80ca43deafc40090c910eb4b35 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Tue, 3 Feb 2009 14:51:00 +0000 Subject: Fixed a bug where the end character was being added when the character limit's limit intersected the last word of the string. http://expressionengine.com/forums/viewthread/103748/ --- system/helpers/text_helper.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index e79a2419d..fa1de8bc6 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -87,14 +87,16 @@ if ( ! function_exists('character_limiter')) { return $str; } - + $out = ""; foreach (explode(' ', trim($str)) as $val) { - $out .= $val.' '; + $out .= $val.' '; + if (strlen($out) >= $n) { - return trim($out).$end_char; + $out = trim($out); + return (strlen($out) == strlen($str)) ? $out : $out.$end_char; } } } @@ -236,7 +238,7 @@ if ( ! function_exists('word_censor')) // \w, \b and a few others do not match on a unicode character // set for performance reasons. As a result words like über // will not match on a word boundary. Instead, we'll assume that - // a bad word will be bookeneded by any of these characters. + // a bad word will be bookended by any of these characters. $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]'; foreach ($censored as $badword) -- cgit v1.2.3-24-g4f1b From 1978e12d4221fe7e61749a3206b086e5d4158f77 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Tue, 3 Feb 2009 14:54:43 +0000 Subject: tweak to ascii_to_entities() for low ascii entities --- system/helpers/text_helper.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index fa1de8bc6..ad051cd76 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -127,7 +127,17 @@ if ( ! function_exists('ascii_to_entities')) if ($ordinal < 128) { - $out .= $str[$i]; + /* + If the $temp array has a value but we have moved on, then it seems only + fair that we output that entity and restart $temp before continuing. -Paul + */ + if (count($temp) == 1) + { + $out .= '&#'.array_shift($temp).';'; + $count = 1; + } + + $out .= $str[$i]; } else { -- cgit v1.2.3-24-g4f1b From fc395a1046441cb584cbcfe42651dacece7eca3e Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 22 Apr 2009 14:15:09 +0000 Subject: updated copyrights to 2009 --- system/helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index ad051cd76..06910e411 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -6,7 +6,7 @@ * * @package CodeIgniter * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2008, EllisLab, Inc. + * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 -- cgit v1.2.3-24-g4f1b From 7f3719faf120dc15f3d7b45e132ab3192f60ad62 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Tue, 5 Jan 2010 13:35:37 +0000 Subject: updated copyrights --- system/helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 06910e411..1e672937b 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -6,7 +6,7 @@ * * @package CodeIgniter * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc. + * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 -- cgit v1.2.3-24-g4f1b From daf9c01a2c25515eea87fb4f4c905f49c30d0214 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Fri, 5 Mar 2010 10:29:30 -0600 Subject: added convert_accented_characters() function to Text Helper --- system/helpers/text_helper.php | 48 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 1e672937b..477260216 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -87,7 +87,7 @@ if ( ! function_exists('character_limiter')) { return $str; } - + $out = ""; foreach (explode(' ', trim($str)) as $val) { @@ -136,7 +136,7 @@ if ( ! function_exists('ascii_to_entities')) $out .= '&#'.array_shift($temp).';'; $count = 1; } - + $out .= $str[$i]; } else @@ -248,7 +248,7 @@ if ( ! function_exists('word_censor')) // \w, \b and a few others do not match on a unicode character // set for performance reasons. As a result words like über // will not match on a word boundary. Instead, we'll assume that - // a bad word will be bookended by any of these characters. + // a bad word will be bookeneded by any of these characters. $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]'; foreach ($censored as $badword) @@ -352,6 +352,44 @@ if ( ! function_exists('highlight_phrase')) return $str; } } + +// ------------------------------------------------------------------------ + +/** + * Convert Accented Foreign Characters to ASCII + * + * @access public + * @param string the text string + * @return string + */ +if ( ! function_exists('convert_accented_characters')) +{ + function convert_accented_characters($match) + { + if ( ! file_exists(APPPATH.'config/foreign_chars'.EXT)) + { + return $match; + } + + include APPPATH.'config/foreign_chars'.EXT; + + if ( ! isset($foreign_characters)) + { + return $match; + } + + $ord = ord($match['1']); + + if (isset($foreign_characters[$ord])) + { + return $foreign_characters[$ord]; + } + else + { + return $match['1']; + } + } +} // ------------------------------------------------------------------------ @@ -431,7 +469,7 @@ if ( ! function_exists('word_wrap')) // word into smaller chunks so we'll add it back to our current line if ($temp != '') { - $output .= $temp . "\n" . $line; + $output .= $temp."\n".$line; } else { @@ -456,7 +494,7 @@ if ( ! function_exists('word_wrap')) return $output; } } - + /* End of file text_helper.php */ /* Location: ./system/helpers/text_helper.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From cbe3247819be75c34231ea200874044735bd853b Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Thu, 5 Aug 2010 14:09:20 -0500 Subject: Adding an ellipsize function to the text helper, and associated documentation to the user guide. --- system/helpers/text_helper.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 477260216..b7ade7a8f 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -495,6 +495,48 @@ if ( ! function_exists('word_wrap')) } } +// ------------------------------------------------------------------------ + +if ( ! function_exists('ellipsize')) +{ + /** + * Ellipsize String + * + * This function will strip tags from a string, split it at its max_length and ellipsize + * + * @param string string to ellipsize + * @param integer max length of string + * @param mixed int (1|0) or float, .5, .2, etc for position to split + * @param string ellipsis ; Default '...' + * @return string ellipsized string + */ + function ellipsize($str, $max_length, $position = 1, $ellipsis = '…') + { + // Strip tags + $str = trim(strip_tags($str)); + + // Is the string long enough to ellipsize? + if (strlen($str) <= $max_length) + { + return $str; + } + + $beg = substr($str, 0, floor($max_length * $position)); + + $position = ($position > 1) ? 1 : $position; + + if ($position === 1) + { + $end = substr($str, 0, -($max_length - strlen($beg))); + } + else + { + $end = substr($str, -($max_length - strlen($beg))); + } + + return $beg.$ellipsis.$end; + } +} /* End of file text_helper.php */ /* Location: ./system/helpers/text_helper.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 0f6b7c1ce0f0122ba162dc579a34a73438d4955a Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Thu, 5 Aug 2010 14:11:14 -0500 Subject: moving code comment in text helper --- system/helpers/text_helper.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index b7ade7a8f..b85e2aaef 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -497,19 +497,19 @@ if ( ! function_exists('word_wrap')) // ------------------------------------------------------------------------ +/** + * Ellipsize String + * + * This function will strip tags from a string, split it at its max_length and ellipsize + * + * @param string string to ellipsize + * @param integer max length of string + * @param mixed int (1|0) or float, .5, .2, etc for position to split + * @param string ellipsis ; Default '...' + * @return string ellipsized string + */ if ( ! function_exists('ellipsize')) { - /** - * Ellipsize String - * - * This function will strip tags from a string, split it at its max_length and ellipsize - * - * @param string string to ellipsize - * @param integer max length of string - * @param mixed int (1|0) or float, .5, .2, etc for position to split - * @param string ellipsis ; Default '...' - * @return string ellipsized string - */ function ellipsize($str, $max_length, $position = 1, $ellipsis = '…') { // Strip tags -- cgit v1.2.3-24-g4f1b From dd6719738936be31cdaa1758ca86d5eb14dcab3d Mon Sep 17 00:00:00 2001 From: Barry Mieny Date: Mon, 4 Oct 2010 16:33:58 +0200 Subject: Cleanup of stray spaces and tabs --- system/helpers/text_helper.php | 264 ++++++++++++++++++++--------------------- 1 file changed, 132 insertions(+), 132 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index b85e2aaef..8bc1cd5e5 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -37,7 +37,7 @@ * @param integer * @param string the end character. Usually an ellipsis * @return string - */ + */ if ( ! function_exists('word_limiter')) { function word_limiter($str, $limit = 100, $end_char = '…') @@ -46,18 +46,18 @@ if ( ! function_exists('word_limiter')) { return $str; } - + preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches); - + if (strlen($str) == strlen($matches[0])) { $end_char = ''; } - + return rtrim($matches[0]).$end_char; } } - + // ------------------------------------------------------------------------ /** @@ -71,7 +71,7 @@ if ( ! function_exists('word_limiter')) * @param integer * @param string the end character. Usually an ellipsis * @return string - */ + */ if ( ! function_exists('character_limiter')) { function character_limiter($str, $n = 500, $end_char = '…') @@ -80,28 +80,28 @@ if ( ! function_exists('character_limiter')) { return $str; } - + $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str)); if (strlen($str) <= $n) { return $str; } - + $out = ""; foreach (explode(' ', trim($str)) as $val) { $out .= $val.' '; - + if (strlen($out) >= $n) { $out = trim($out); return (strlen($out) == strlen($str)) ? $out : $out.$end_char; - } + } } } } - + // ------------------------------------------------------------------------ /** @@ -112,21 +112,21 @@ if ( ! function_exists('character_limiter')) * @access public * @param string * @return string - */ + */ if ( ! function_exists('ascii_to_entities')) { function ascii_to_entities($str) { - $count = 1; - $out = ''; - $temp = array(); - - for ($i = 0, $s = strlen($str); $i < $s; $i++) - { - $ordinal = ord($str[$i]); - - if ($ordinal < 128) - { + $count = 1; + $out = ''; + $temp = array(); + + for ($i = 0, $s = strlen($str); $i < $s; $i++) + { + $ordinal = ord($str[$i]); + + if ($ordinal < 128) + { /* If the $temp array has a value but we have moved on, then it seems only fair that we output that entity and restart $temp before continuing. -Paul @@ -136,33 +136,33 @@ if ( ! function_exists('ascii_to_entities')) $out .= '&#'.array_shift($temp).';'; $count = 1; } - + $out .= $str[$i]; - } - else - { - if (count($temp) == 0) - { - $count = ($ordinal < 224) ? 2 : 3; - } - - $temp[] = $ordinal; - - if (count($temp) == $count) - { - $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); - - $out .= '&#'.$number.';'; - $count = 1; - $temp = array(); - } - } - } - - return $out; + } + else + { + if (count($temp) == 0) + { + $count = ($ordinal < 224) ? 2 : 3; + } + + $temp[] = $ordinal; + + if (count($temp) == $count) + { + $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); + + $out .= '&#'.$number.';'; + $count = 1; + $temp = array(); + } + } + } + + return $out; } } - + // ------------------------------------------------------------------------ /** @@ -174,51 +174,51 @@ if ( ! function_exists('ascii_to_entities')) * @param string * @param bool * @return string - */ + */ if ( ! function_exists('entities_to_ascii')) { function entities_to_ascii($str, $all = TRUE) { - if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) - { - for ($i = 0, $s = count($matches['0']); $i < $s; $i++) - { - $digits = $matches['1'][$i]; - - $out = ''; - - if ($digits < 128) - { - $out .= chr($digits); - - } - elseif ($digits < 2048) - { - $out .= chr(192 + (($digits - ($digits % 64)) / 64)); - $out .= chr(128 + ($digits % 64)); - } - else - { - $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); - $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); - $out .= chr(128 + ($digits % 64)); - } - - $str = str_replace($matches['0'][$i], $out, $str); - } - } - - if ($all) - { - $str = str_replace(array("&", "<", ">", """, "'", "-"), - array("&","<",">","\"", "'", "-"), - $str); - } - - return $str; + if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) + { + for ($i = 0, $s = count($matches['0']); $i < $s; $i++) + { + $digits = $matches['1'][$i]; + + $out = ''; + + if ($digits < 128) + { + $out .= chr($digits); + + } + elseif ($digits < 2048) + { + $out .= chr(192 + (($digits - ($digits % 64)) / 64)); + $out .= chr(128 + ($digits % 64)); + } + else + { + $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); + $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); + $out .= chr(128 + ($digits % 64)); + } + + $str = str_replace($matches['0'][$i], $out, $str); + } + } + + if ($all) + { + $str = str_replace(array("&", "<", ">", """, "'", "-"), + array("&","<",">","\"", "'", "-"), + $str); + } + + return $str; } } - + // ------------------------------------------------------------------------ /** @@ -233,7 +233,7 @@ if ( ! function_exists('entities_to_ascii')) * @param string the array of censoered words * @param string the optional replacement value * @return string - */ + */ if ( ! function_exists('word_censor')) { function word_censor($str, $censored, $replacement = '') @@ -242,8 +242,8 @@ if ( ! function_exists('word_censor')) { return $str; } - - $str = ' '.$str.' '; + + $str = ' '.$str.' '; // \w, \b and a few others do not match on a unicode character // set for performance reasons. As a result words like über @@ -263,10 +263,10 @@ if ( ! function_exists('word_censor')) } } - return trim($str); + return trim($str); } } - + // ------------------------------------------------------------------------ /** @@ -277,26 +277,26 @@ if ( ! function_exists('word_censor')) * @access public * @param string the text string * @return string - */ + */ if ( ! function_exists('highlight_code')) { function highlight_code($str) - { + { // The highlight string function encodes and highlights // brackets so we need them to start raw $str = str_replace(array('<', '>'), array('<', '>'), $str); - + // Replace any existing PHP tags to temporary markers so they don't accidentally // break the string out of PHP, and thus, thwart the highlighting. - - $str = str_replace(array('', '<%', '%>', '\\', ''), + + $str = str_replace(array('', '<%', '%>', '\\', ''), array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str); // The highlight_string function requires that the text be surrounded // by PHP tags, which we will remove later $str = ''; // tags @@ -307,20 +307,20 @@ if ( ! function_exists('highlight_code')) $str = str_replace(array(''), array(''), $str); $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); } - + // Remove our artificially added PHP, and the syntax highlighting that came with it $str = preg_replace('/<\?php( | )/i', '', $str); $str = preg_replace('/(.*?)\?><\/span>\n<\/span>\n<\/code>/is', "$1\n\n", $str); $str = preg_replace('/<\/span>/i', '', $str); - + // Replace our markers back to PHP tags. $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), array('<?', '?>', '<%', '%>', '\\', '</script>'), $str); - + return $str; } } - + // ------------------------------------------------------------------------ /** @@ -334,7 +334,7 @@ if ( ! function_exists('highlight_code')) * @param string the openging tag to precede the phrase with * @param string the closing tag to end the phrase with * @return string - */ + */ if ( ! function_exists('highlight_phrase')) { function highlight_phrase($str, $phrase, $tag_open = '', $tag_close = '') @@ -343,7 +343,7 @@ if ( ! function_exists('highlight_phrase')) { return ''; } - + if ($phrase != '') { return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str); @@ -361,7 +361,7 @@ if ( ! function_exists('highlight_phrase')) * @access public * @param string the text string * @return string - */ + */ if ( ! function_exists('convert_accented_characters')) { function convert_accented_characters($match) @@ -370,16 +370,16 @@ if ( ! function_exists('convert_accented_characters')) { return $match; } - + include APPPATH.'config/foreign_chars'.EXT; - + if ( ! isset($foreign_characters)) { return $match; } - + $ord = ord($match['1']); - + if (isset($foreign_characters[$ord])) { return $foreign_characters[$ord]; @@ -390,7 +390,7 @@ if ( ! function_exists('convert_accented_characters')) } } } - + // ------------------------------------------------------------------------ /** @@ -404,7 +404,7 @@ if ( ! function_exists('convert_accented_characters')) * @param string the text string * @param integer the number of characters to wrap at * @return string - */ + */ if ( ! function_exists('word_wrap')) { function word_wrap($str, $charlim = '76') @@ -412,47 +412,47 @@ if ( ! function_exists('word_wrap')) // Se the character limit if ( ! is_numeric($charlim)) $charlim = 76; - + // Reduce multiple spaces $str = preg_replace("| +|", " ", $str); - + // Standardize newlines if (strpos($str, "\r") !== FALSE) { - $str = str_replace(array("\r\n", "\r"), "\n", $str); + $str = str_replace(array("\r\n", "\r"), "\n", $str); } - - // If the current word is surrounded by {unwrap} tags we'll + + // If the current word is surrounded by {unwrap} tags we'll // strip the entire chunk and replace it with a marker. $unwrap = array(); if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) { for ($i = 0; $i < count($matches['0']); $i++) { - $unwrap[] = $matches['1'][$i]; + $unwrap[] = $matches['1'][$i]; $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); } } - - // Use PHP's native function to do the initial wordwrap. - // We set the cut flag to FALSE so that any individual words that are + + // Use PHP's native function to do the initial wordwrap. + // We set the cut flag to FALSE so that any individual words that are // too long get left alone. In the next step we'll deal with them. $str = wordwrap($str, $charlim, "\n", FALSE); - + // Split the string into individual lines of text and cycle through them $output = ""; - foreach (explode("\n", $str) as $line) + foreach (explode("\n", $str) as $line) { // Is the line within the allowed character count? // If so we'll join it to the output and continue if (strlen($line) <= $charlim) { - $output .= $line."\n"; + $output .= $line."\n"; continue; } - + $temp = ''; - while((strlen($line)) > $charlim) + while((strlen($line)) > $charlim) { // If the over-length word is a URL we won't wrap it if (preg_match("!\[url.+\]|://|wwww.!", $line)) @@ -464,12 +464,12 @@ if ( ! function_exists('word_wrap')) $temp .= substr($line, 0, $charlim-1); $line = substr($line, $charlim-1); } - - // If $temp contains data it means we had to split up an over-length + + // If $temp contains data it means we had to split up an over-length // word into smaller chunks so we'll add it back to our current line if ($temp != '') { - $output .= $temp."\n".$line; + $output .= $temp."\n".$line; } else { @@ -481,7 +481,7 @@ if ( ! function_exists('word_wrap')) // Put our markers back if (count($unwrap) > 0) - { + { foreach ($unwrap as $key => $val) { $output = str_replace("{{unwrapped".$key."}}", $val, $output); @@ -491,10 +491,10 @@ if ( ! function_exists('word_wrap')) // Remove the unwrap tags $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); - return $output; + return $output; } } - + // ------------------------------------------------------------------------ /** @@ -502,10 +502,10 @@ if ( ! function_exists('word_wrap')) * * This function will strip tags from a string, split it at its max_length and ellipsize * - * @param string string to ellipsize + * @param string string to ellipsize * @param integer max length of string * @param mixed int (1|0) or float, .5, .2, etc for position to split - * @param string ellipsis ; Default '...' + * @param string ellipsis ; Default '...' * @return string ellipsized string */ if ( ! function_exists('ellipsize')) @@ -534,7 +534,7 @@ if ( ! function_exists('ellipsize')) $end = substr($str, -($max_length - strlen($beg))); } - return $beg.$ellipsis.$end; + return $beg.$ellipsis.$end; } } -- cgit v1.2.3-24-g4f1b From 741de1c1319dd13de75348863cca591713dd46ce Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Wed, 10 Nov 2010 14:52:57 -0600 Subject: Updating PHP requirements in files 5.1.6 --- system/helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 8bc1cd5e5..58b08aaf8 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -2,7 +2,7 @@ /** * CodeIgniter * - * An open source application development framework for PHP 4.3.2 or newer + * An open source application development framework for PHP 5.1.6 or newer * * @package CodeIgniter * @author ExpressionEngine Dev Team -- cgit v1.2.3-24-g4f1b From 0711dc87d98ce20d3a87f7ac43d78af8fba1dca7 Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Wed, 5 Jan 2011 10:49:40 -0600 Subject: Hey look, it's 2011 --- system/helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 58b08aaf8..e1b56c9c9 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -6,7 +6,7 @@ * * @package CodeIgniter * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. + * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 -- cgit v1.2.3-24-g4f1b From 5e04480e5a4dd1b9b639eca54f5f3adbd0b39039 Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Tue, 11 Jan 2011 16:10:26 -0500 Subject: Reworked convert_foreign_characters in text helper. Thanks to Mario Ricalde. --- system/helpers/text_helper.php | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 58b08aaf8..9194b577d 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -364,30 +364,21 @@ if ( ! function_exists('highlight_phrase')) */ if ( ! function_exists('convert_accented_characters')) { - function convert_accented_characters($match) + function convert_accented_characters($str) { if ( ! file_exists(APPPATH.'config/foreign_chars'.EXT)) { - return $match; + return $str; } include APPPATH.'config/foreign_chars'.EXT; if ( ! isset($foreign_characters)) { - return $match; + return $str; } - $ord = ord($match['1']); - - if (isset($foreign_characters[$ord])) - { - return $foreign_characters[$ord]; - } - else - { - return $match['1']; - } + return preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str); } } -- cgit v1.2.3-24-g4f1b From 45e3cdf52d7438c5a6adf70835d96cfeab1eea0e Mon Sep 17 00:00:00 2001 From: Pascal Kriete Date: Mon, 14 Feb 2011 13:26:20 -0500 Subject: Whitespace cleanup for code consistency --- system/helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 47e6ccc93..96afd4cee 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -443,7 +443,7 @@ if ( ! function_exists('word_wrap')) } $temp = ''; - while((strlen($line)) > $charlim) + while ((strlen($line)) > $charlim) { // If the over-length word is a URL we won't wrap it if (preg_match("!\[url.+\]|://|wwww.!", $line)) -- cgit v1.2.3-24-g4f1b From 0ea04149bbae0fdcde92b7362e7cbd76f0df3865 Mon Sep 17 00:00:00 2001 From: bubbafoley Date: Thu, 17 Mar 2011 14:55:41 -0500 Subject: load config files from environment specific locations in core classes, helpers and libraries --- system/helpers/text_helper.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 96afd4cee..197bcb14e 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -366,12 +366,19 @@ if ( ! function_exists('convert_accented_characters')) { function convert_accented_characters($str) { - if ( ! file_exists(APPPATH.'config/foreign_chars'.EXT)) + if ( ! file_exists(APPPATH.'config/foreign_chars'.EXT) AND ! file_exists(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT)) { return $str; } - include APPPATH.'config/foreign_chars'.EXT; + if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT)) + { + include APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT; + } + else + { + include APPPATH.'config/foreign_chars'.EXT; + } if ( ! isset($foreign_characters)) { -- cgit v1.2.3-24-g4f1b From 928083406322821a35a7d8a4205620c3854772a6 Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Fri, 18 Mar 2011 09:02:37 -0400 Subject: Fixed coding to match standards from previous releases --- system/helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 197bcb14e..99f521fb5 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -373,7 +373,7 @@ if ( ! function_exists('convert_accented_characters')) if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT)) { - include APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT; + include APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT; } else { -- cgit v1.2.3-24-g4f1b From fdd5b11b62f127901ddff2e5dc7923b063371070 Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Mon, 21 Mar 2011 21:28:58 -0400 Subject: Fixed logic and removed the error supressing --- system/helpers/text_helper.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 99f521fb5..664408912 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -366,18 +366,13 @@ if ( ! function_exists('convert_accented_characters')) { function convert_accented_characters($str) { - if ( ! file_exists(APPPATH.'config/foreign_chars'.EXT) AND ! file_exists(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT)) + if (is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT)) { - return $str; + include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT); } - - if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT)) - { - include APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT; - } - else + elseif (is_file(APPPATH.'config/foreign_chars'.EXT)) { - include APPPATH.'config/foreign_chars'.EXT; + include(APPPATH.'config/foreign_chars'.EXT); } if ( ! isset($foreign_characters)) -- cgit v1.2.3-24-g4f1b From 05fa61144667c85b0463f7e8baa6af00aa195dc6 Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Wed, 6 Apr 2011 22:57:43 +0100 Subject: Made Environment Support optional. Comment out or delete the constant to stop environment checks. --- system/helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 664408912..cca093976 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -366,7 +366,7 @@ if ( ! function_exists('convert_accented_characters')) { function convert_accented_characters($str) { - if (is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT)) + if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT)) { include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT); } -- cgit v1.2.3-24-g4f1b From 3a746655e92ec59ee7e731c3535673a9aedc5d3e Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Tue, 19 Apr 2011 10:59:47 -0500 Subject: Removing internal references to the EXT constant. Additionally, marked the constant as deprecated. Use ".php" instead. Also adding upgrade notes from 2.0.2 to 2.0.3. --- system/helpers/text_helper.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index cca093976..33d7fa2fd 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -366,13 +366,13 @@ if ( ! function_exists('convert_accented_characters')) { function convert_accented_characters($str) { - if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT)) + if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php')) { - include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars'.EXT); + include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'); } - elseif (is_file(APPPATH.'config/foreign_chars'.EXT)) + elseif (is_file(APPPATH.'config/foreign_chars.php')) { - include(APPPATH.'config/foreign_chars'.EXT); + include(APPPATH.'config/foreign_chars.php'); } if ( ! isset($foreign_characters)) -- cgit v1.2.3-24-g4f1b From 114ab0988e20ac6be39ad363ff897a1a3b85e565 Mon Sep 17 00:00:00 2001 From: Razican Date: Mon, 25 Apr 2011 17:26:45 +0200 Subject: Fixed double-space typo. --- system/helpers/text_helper.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 33d7fa2fd..7d621257f 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -1,4 +1,4 @@ - Date: Fri, 1 Jul 2011 17:40:48 -0500 Subject: backed out 648b42a75739, which was a NON-trivial whitespace commit. It broke the Typography class's string replacements, for instance --- system/helpers/text_helper.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 7d621257f..33d7fa2fd 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -1,4 +1,4 @@ -