Infected.Shadow Posted February 16, 2012 Share Posted February 16, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/257120-bbcode-code-tag-syntax-highlighting/ Share on other sites More sharing options...
requinix Posted February 16, 2012 Share Posted February 16, 2012 If you want preg_replace() to treat a string as PHP code to evaluate then you need to use the /e flag in the search expression. Then make the replacement string be the code you want executed. Also, you really shouldn't be HTML encoding anything until it's about to be displayed. However that's the general case: in your situation you should have two separate fields in your database table for the post text. One of them is the original text so you don't have to reverse-engineer it, and the other is the HTML-ready text so you don't have to parse BBCode and such every time you want to display it. Quote Link to comment https://forums.phpfreaks.com/topic/257120-bbcode-code-tag-syntax-highlighting/#findComment-1318085 Share on other sites More sharing options...
scootstah Posted February 16, 2012 Share Posted February 16, 2012 Also, you really shouldn't be HTML encoding anything until it's about to be displayed. However that's the general case: in your situation you should have two separate fields in your database table for the post text. One of them is the original text so you don't have to reverse-engineer it, and the other is the HTML-ready text so you don't have to parse BBCode and such every time you want to display it. While handy, that's going to take significantly more space to store. Quote Link to comment https://forums.phpfreaks.com/topic/257120-bbcode-code-tag-syntax-highlighting/#findComment-1318088 Share on other sites More sharing options...
Infected.Shadow Posted February 16, 2012 Author Share Posted February 16, 2012 If you want preg_replace() to treat a string as PHP code to evaluate then you need to use the /e flag in the search expression. Then make the replacement string be the code you want executed. Also, you really shouldn't be HTML encoding anything until it's about to be displayed. However that's the general case: in your situation you should have two separate fields in your database table for the post text. One of them is the original text so you don't have to reverse-engineer it, and the other is the HTML-ready text so you don't have to parse BBCode and such every time you want to display it. Yeah I ended up finding an example in the comments of highlight_string on the php manual. As for the other point: That's something I never even thought of, so thank you for that idea. I'd been encoding it as I stored it because I figured that was more secure at the time (which I recall was about 4am when the caffeine was wearing off ) Quote Link to comment https://forums.phpfreaks.com/topic/257120-bbcode-code-tag-syntax-highlighting/#findComment-1318090 Share on other sites More sharing options...
scootstah Posted February 16, 2012 Share Posted February 16, 2012 The problem with the way you are doing it is what if they need to edit it? They are going to get back HTML instead of BBCode, unless you convert it back. Quote Link to comment https://forums.phpfreaks.com/topic/257120-bbcode-code-tag-syntax-highlighting/#findComment-1318092 Share on other sites More sharing options...
Infected.Shadow Posted February 16, 2012 Author Share Posted February 16, 2012 Also, you really shouldn't be HTML encoding anything until it's about to be displayed. However that's the general case: in your situation you should have two separate fields in your database table for the post text. One of them is the original text so you don't have to reverse-engineer it, and the other is the HTML-ready text so you don't have to parse BBCode and such every time you want to display it. While handy, that's going to take significantly more space to store. Takes more space in the database yes, but usually that's never going to be a big issue. Though a better way might be store the original text in the database then implement some sort of caching system to store the rendered text that way you just take out the time of querying the database if there are no new posts. The problem with the way you are doing it is what if they need to edit it? They are going to get back HTML instead of BBCode, unless you convert it back. In the edit form leave out parseBBCode. Problem solved... Quote Link to comment https://forums.phpfreaks.com/topic/257120-bbcode-code-tag-syntax-highlighting/#findComment-1318093 Share on other sites More sharing options...
scootstah Posted February 16, 2012 Share Posted February 16, 2012 The problem with the way you are doing it is what if they need to edit it? They are going to get back HTML instead of BBCode, unless you convert it back. In the edit form leave out parseBBCode. Problem solved... Oops, my skim reading led me to believe you were storing it in the database after you parsed it. Carry on. Quote Link to comment https://forums.phpfreaks.com/topic/257120-bbcode-code-tag-syntax-highlighting/#findComment-1318095 Share on other sites More sharing options...
Infected.Shadow Posted February 16, 2012 Author Share Posted February 16, 2012 The problem with the way you are doing it is what if they need to edit it? They are going to get back HTML instead of BBCode, unless you convert it back. In the edit form leave out parseBBCode. Problem solved... Oops, my skim reading led me to believe you were storing it in the database after you parsed it. Carry on. Haha, nope. Just rendering. Quote Link to comment https://forums.phpfreaks.com/topic/257120-bbcode-code-tag-syntax-highlighting/#findComment-1318096 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.