From 4921fed6c17a54efd4fac0bed4d058463bd9b601 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sat, 7 Jan 2012 01:28:07 +0200 Subject: Improve the smiley, string & text helpers --- system/helpers/text_helper.php | 133 +++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 70 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 842a31d75..562270f96 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -1,13 +1,13 @@ -","\"", "'", "-"), - $str); + return str_replace(array('&', '<', '>', '"', ''', '-'), + array('&', '<', '>', '"', "'", '-'), + $str); } return $str; @@ -294,42 +294,38 @@ 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 encodes and highlights + * brackets so we need them to start raw. + * + * Also 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); - } + $str = highlight_string('', TRUE); // 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); + $str = preg_replace(array( + '/<\?php( | )/i', + '/(.*?)\?><\/span>\n<\/span>\n<\/code>/is', + '/<\/span>/i' + ), + array( + '', + "$1\n\n", + '' + ), + $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_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), + array('<?', '?>', '<%', '%>', '\\', '</script>'), + $str); } } @@ -414,12 +410,14 @@ if ( ! function_exists('word_wrap')) { function word_wrap($str, $charlim = '76') { - // Se the character limit + // Set the character limit if ( ! is_numeric($charlim)) + { $charlim = 76; + } // Reduce multiple spaces - $str = preg_replace("| +|", " ", $str); + $str = preg_replace('| +|', ' ', $str); // Standardize newlines if (strpos($str, "\r") !== FALSE) @@ -430,22 +428,22 @@ if ( ! function_exists('word_wrap')) // 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)) + if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches)) { - for ($i = 0; $i < count($matches['0']); $i++) + for ($i = 0, $c = count($matches[0]); $i < $c; $i++) { - $unwrap[] = $matches['1'][$i]; - $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); + $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. + // 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 = ""; + $output = ''; foreach (explode("\n", $str) as $line) { // Is the line within the allowed character count? @@ -460,7 +458,7 @@ if ( ! function_exists('word_wrap')) while ((strlen($line)) > $charlim) { // If the over-length word is a URL we won't wrap it - if (preg_match("!\[url.+\]|://|wwww.!", $line)) + if (preg_match('!\[url.+\]|://|wwww.!', $line)) { break; } @@ -474,14 +472,12 @@ 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."\n"; } else { - $output .= $line; + $output .= $line."\n"; } - - $output .= "\n"; } // Put our markers back @@ -489,14 +485,12 @@ if ( ! function_exists('word_wrap')) { foreach ($unwrap as $key => $val) { - $output = str_replace("{{unwrapped".$key."}}", $val, $output); + $output = str_replace('{{unwrapped'.$key.'}}', $val, $output); } } - // Remove the unwrap tags - $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); - - return $output; + // Remove the unwrap tags and return + return str_replace(array('{unwrap}', '{/unwrap}'), '', $output); } } @@ -527,7 +521,6 @@ if ( ! function_exists('ellipsize')) } $beg = substr($str, 0, floor($max_length * $position)); - $position = ($position > 1) ? 1 : $position; if ($position === 1) @@ -544,4 +537,4 @@ if ( ! function_exists('ellipsize')) } /* End of file text_helper.php */ -/* Location: ./system/helpers/text_helper.php */ \ No newline at end of file +/* Location: ./system/helpers/text_helper.php */ -- cgit v1.2.3-24-g4f1b