TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   BBCode preg_match => str_replace (http://www.talkphp.com/advanced-php-programming/1940-bbcode-preg_match-str_replace.html)

ReSpawN 01-11-2008 08:15 PM

BBCode preg_match => str_replace
 
G'd evening guys,

I'm trying to get a BBCode editor to work.

In code tags for PHP, I've got this;

PHP Code:

[b]This text is bold.[/b]
[
code]This code [b]however[/bis not boldsince we want to see the code.[/code

What is the best way, to recognize if there is any BBCode between the code tags and not transform them, but show them as their are instead of making them strong.

I myself thought first of str_replace, but then you'll need to search into the code tags if ANY BBCode is present...

Anyways, I am out of options so maybe the RegEx specialist or BBCode specialists could help me out.

ReSpawN

RobertK 01-11-2008 08:27 PM

Aha, you've stumbled on the old switcheroo! What do I mean? Well, process [ code ] bbCode first and replace it with a {codeblock_#}, and push the actual code inside the tags to an array with an index of "codeblock_#". To do this you'll have to do a preg_replace with a callback and return the string based off a global variable you initialize to zero for each post. Then at the end use a standard call to:
PHP Code:

return str_replace(array_keys($codeblocks), array_values($codeblocks), $post); 

And tada! No smilies, no HTML, and no wonkiness in your code!8-)

ReSpawN 01-11-2008 08:47 PM

RobertK member of the month February! :P Darn man, you're pretty good at it! Loved your other post about hashes as well. Thanks very much.

RobertK 01-11-2008 08:54 PM

Wow, thanks. :-) I've only been a member a few days, and already my little tricks are well liked.

I'm always glad to help. I noticed that one in SMF, as their code was so easily untouched I was curious as to why. And that happens to be their method. Just placeholder it.

Orc 01-13-2008 11:44 AM

Quote:

Originally Posted by RobertK (Post 8264)
Aha, you've stumbled on the old switcheroo! What do I mean? Well, process [ code ] bbCode first and replace it with a {codeblock_#}, and push the actual code inside the tags to an array with an index of "codeblock_#". To do this you'll have to do a preg_replace with a callback and return the string based off a global variable you initialize to zero for each post. Then at the end use a standard call to:
PHP Code:

return str_replace(array_keys($codeblocks), array_values($codeblocks), $post); 

And tada! No smilies, no HTML, and no wonkiness in your code!8-)

You should wrap that into a function. bbCode(); :]

ReSpawN 01-13-2008 09:41 PM

Could be a nice start for sure. I'll work the concept out in a few days. Let me get back to it on that.


All times are GMT. The time now is 02:15 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0