freak127 Posted February 25, 2006 Share Posted February 25, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/3589-bbcode-help-before-i-go-insane/ Share on other sites More sharing options...
Pezzoni Posted February 25, 2006 Share Posted February 25, 2006 With code tags, do you not need to think about not parsing BBCode contained within them, as well? Quote Link to comment https://forums.phpfreaks.com/topic/3589-bbcode-help-before-i-go-insane/#findComment-12430 Share on other sites More sharing options...
freak127 Posted February 25, 2006 Author Share Posted February 25, 2006 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 Link to comment https://forums.phpfreaks.com/topic/3589-bbcode-help-before-i-go-insane/#findComment-12431 Share on other sites More sharing options...
obsidian Posted February 25, 2006 Share Posted February 25, 2006 [!--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('<','>','<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] Quote Link to comment https://forums.phpfreaks.com/topic/3589-bbcode-help-before-i-go-insane/#findComment-12436 Share on other sites More sharing options...
freak127 Posted February 25, 2006 Author Share Posted February 25, 2006 Ah, thank you so much Obsidian :D The snippet works great, exactly what I was looking for.Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/3589-bbcode-help-before-i-go-insane/#findComment-12437 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.