Jump to content

BBC Code Help Me Before I Go Bonkers


The-Last-Escape

Recommended Posts

I'm currently working on a CMS and I'm doing the BBCode functions for it at the moment. Actually, I've been doing one part of the function for the last three days, and it has been driving me slowly but surely mental. Whatever I do, I CANNOT get this to work.

I'll post the code I'm currently using, then try to explain my problem:

 

 

Code:

<?php
function highlight($txt="")
{
    $txt = html_entity_decode($txt, ENT_QUOTES);
    $txt = "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\"><tr><td class=\"code\"><div class=\"title\">Code</div><div class=\"content\">".highlight_string($txt, true)."</div></td></tr></table>";
    return $txt;
}

function bbcode($message)
{
    $message = preg_replace("'\n'", "<br>", $message);
    $message = preg_replace("'\[b\](.*?)\[/b\]'", "<b>$1</b>", $message);
    $message = preg_replace("'\[i\](.*?)\[/i\]'", "<i>$1</i>", $message);
    $message = preg_replace("'\[u\](.*?)\[/u\]'", "<u>$1</u>", $message);
    $message = preg_replace("'\[code\](.*?)\[/code\]'", highlight('$1'), $message);

    return $message;
}
?>

 

That's my function.php file, which is included in every single page. Anyway, it is the code tags ([*code][*/code] - without the asterisks) that I am having problems with. I've tried various ways of doing this, and so far none of them have worked. This is the one I have got at the moment, and it is the closest I have gotten to a working function.

Anyway, the problem is that when the posted message is shown, it either shows '$1' instead of the text between the code tags, or it shows the text between the code tags, but not highlighted. The thing that is driving me to insanity is that even if the message put through the function is the same, it comes out differently at the end. For example, I might enter [*code]<? phpinfo(); ?>[*/code] once, and it will display '$1' (without the quotes), then if I enter the exact same message, it might display '<? phpinfo(); ?>', but not highlighted at all.

I really have no idea what is wrong with it now, so any help at all that anyone can offer would be amazing.

Thanks so much.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/49326-bbc-code-help-me-before-i-go-bonkers/
Share on other sites

thorpe, that errored out for me. It does however work in its original state.

 

However, I do believe it's a problem with highlight_string itself and not your code, as I had exactly the same problem when I was writing an error handling routine. You can just refresh the page and whether it highlights or not is hit and miss...

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.