Jump to content

Code highlighting


SharkBait

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.