Jump to content


Photo

BBCode help, before I go insane!


  • Please log in to reply
4 replies to this topic

#1 freak127

freak127
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 25 February 2006 - 04:18 PM

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

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

#2 Pezzoni

Pezzoni
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 25 February 2006 - 04:20 PM

With code tags, do you not need to think about not parsing BBCode contained within them, as well?


#3 freak127

freak127
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 25 February 2006 - 04:25 PM

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 ;)

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 25 February 2006 - 04:59 PM

[!--quoteo(post=349325:date=Feb 25 2006, 11:25 AM:name=freak127)--][div class=\'quotetop\']QUOTE(freak127 @ Feb 25 2006, 11:25 AM) View Post[/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.
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;
}


You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 freak127

freak127
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 25 February 2006 - 05:24 PM

Ah, thank you so much Obsidian :D
The snippet works great, exactly what I was looking for.
Thanks again.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users