Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/257120-bbcode-code-tag-syntax-highlighting/
Share on other sites

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.

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.

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 :P )

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...

 

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.

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. :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.