diff --git a/src/LaravelBook/Laravel4Powerpack/Str.php b/src/LaravelBook/Laravel4Powerpack/Str.php index b3190ca..0a3818d 100644 --- a/src/LaravelBook/Laravel4Powerpack/Str.php +++ b/src/LaravelBook/Laravel4Powerpack/Str.php @@ -552,4 +552,44 @@ public static function snake($value, $delimiter = '_') return ctype_lower($value) ? $value : strtolower(preg_replace('/(.)([A-Z])/', $replace, $value)); } + + 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; + } }