I'm attempting to add syntax highlighting to my bbcode function. I started with the general basis for a bbcode function that there are a million tutorials on.
To make things simpler I took out the rest of the bbcode tags.
<?php
//Eventually will need to add some logic to color the code tags
function parseBBCode($string)
{
$search = array(
'/\[code\](.*?)\[\/code\]/is',
);
$replace = array(
'<div class="code"><code>$1</code></div>'
);
//Render the tabs in html
//may need to find a better place for this in the future
$string = str_replace(' ', ' ', $string);
return preg_replace($search, $replace, $string);
}
?>
I've tried using the highlight_string() function but it never highlights it. I assumed this was because when I store the data in the database I used htmlentities() on the text. So I also tried the following to no avail:
'<div class="code">'.highlight_string(html_entity_decode('$1'), true).'</div>'
And this where I'm calling the function:
$posts = '';
while($post = $db->fetchArray($content)) {
$posts .= '<span style="font-size: 22px;">'.$post['title'].'</span><br /> <hr />';
$posts .= '<br />'.nl2br(stripslashes(parseBBCode($post['post']))).'<br /><br /><hr />';
$user = $db->query('SELECT username FROM isnet_users WHERE id = '.$post['poster_id'].';');
$user = $db->fetchArray($user);
$posts .= '<strong>Author: </strong>'.$user['username'].'<br /><br />';
} //end while
I've tried a few other methods that have produced syntax highlighting but have broken other posts that don't need it. So is there something I'm just obliviously missing?