• Top
  • Reply

Syntax Highlighter Code

For us we used the SyntaxHighlighter which can be downloaded at: http://alexgorbatchev.com/wiki/SyntaxHighlighter

to outline syntax in our blog we use co and endco

to do this we first have to make a preg_replace function

//Call to our function that takes a message string as a parameter
function c2pre($message){

    //Return preg_match
    return preg_replace(
        "/(co).*?(endco)/is",                        //Preg Match
        "<pre class='brush: php'> $2 </pre>",        //Replace
        $message);                                   //Input Message

}

Once we have done that then we need to include all the script files in our header

<link type='text/css' rel='stylesheet' href='/style/sh/shCore.css' />
<link type='text/css' rel='stylesheet' href='/style/sh/shThemeDefault.css' />
<script type='text/javascript' src='Scripts/shCore.js'></script>
<script type='text/javascript' src='Scripts/shBrushBash.js'></script>
<script type='text/javascript' src='Scripts/shBrushPhp.js'></script>
<script type='text/javascript'>
//SyntaxHighlighter.config.clipboardSwf = 'Scripts/clipboard.swf';
SyntaxHighlighter.config.stripBrs = true;
SyntaxHighlighter.all();
</script>

Now we use the preg_replace in our query function

$query = mysql_query("SELECT message FROM blog");
$result = mysql_fetch_array($query);

$message = c2pre($result['message']);
print "message";

This should now take all the blogs which include the text co and endco and convert every thing in the middle to code. Happy Coding :D

By

26th Aug 2009
© 2011 Shahmir Javaid - http://shahmirj.com/blog/4


Back to Top
All content is © copyrighted, unless stated otherwise.
Subscribe, @shahmirj, Shahmir Javaid+