SharkBait Posted February 26, 2007 Share Posted February 26, 2007 Ok I have two small functions that will take [code] and put it into a nice little div element for me. Now I am curious would would I go about putting highlight_string() so that the code that goes in (it's usually PHP) becomes highlighted and colour properly? <?php function ConvertToCode($str) { $str = stripslashes($str); $code = array("#\[code\](.*?)\[/code\]#is"); $html = array("<div class=\"code\"><span style=\"font-size:8pt; font-weight:bold;\">Code:</span>\n\\1</div>"); $str = preg_replace($code, $html, $str); // Call dedicated fucntion for replacing quotes $str = doPHPCode($str); return $str; } function doPHPCode($str) { $code = "#\[code\](.*?)\ #is"; $html = "<div class=\"code\"><span style=\"font-size:8pt; font-weight:bold;\">Code:</span>\n\\1</div>"; //when a match is found, replace the match while(preg_match($code, $str)) { $str = preg_replace($code, $html, $str); $str = highlight_string($str); } return $str; } echo ConvertToCode("This is my code <?php echo $myVar; ?> and I would like it highlighted."); ?> [/code] Link to comment https://forums.phpfreaks.com/topic/40179-code-highlighting/ Share on other sites More sharing options...
SharkBait Posted February 27, 2007 Author Share Posted February 27, 2007 Hrmm formatting of the post got a bit buggered since I use [ code ] inside of it No takers on where I would go about or how I would go about using highlight_string() ? Link to comment https://forums.phpfreaks.com/topic/40179-code-highlighting/#findComment-194883 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.