Jump to content

BBCode help, before I go insane!


freak127

Recommended Posts

Hi,
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 [b]CANNOT[/b] get this to work.
I'll post the code I'm currently using, then try to explain my problem:

[code]<?
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;
}
?>[/code]
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 [b]exact[/b] 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
Share on other sites

[!--quoteo(post=349325:date=Feb 25 2006, 11:25 AM:name=freak127)--][div class=\'quotetop\']QUOTE(freak127 @ Feb 25 2006, 11:25 AM) [snapback]349325[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I'm not sure, I didn't think so because the highlight_string() function displays everything in the string as it is. If you mean about ignoring the bold tags and everything, I don't mind at the moment. I just want to get this bit sorted ;)
[/quote]

because of the nature of highlight_string(), for some reason, you can't use it with a backreference within preg_replace() or str_replace() like that. i posted this function for another user with the exact same question a couple days ago, and i hope it helps you, too. this is just a function i came up with when i wrote my BBCode script.
[code]
function PHPEncode($String) {
  preg_match_all("|\[code\].+?\[/code\]|ims", $String, $matches);
  $phpOld = array();
  $phpNew = array();
  foreach ($matches[0] as $orig) {
    $phpOld[] = $orig;
    $new = str_replace(array('&lt;','&gt;','<br />','<br>'), array('<','>',"\n","\n"), $orig);
    $new = trim(preg_replace("|\[code\](.+?)\[/code\]|ims", "$1", $new));
    $phpNew[] = "<div class='snippetTitle'>PHP Code:</div>\n<div class='codeSnippet'>" . highlight_string($new, true) . "</div>\n";
  }

  $String = str_replace($phpOld, $phpNew, $String);
        
  return $String;
}

[/code]
Link to comment
Share on other sites

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.