TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-11-2008, 08:15 PM   #1 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default 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
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 01-11-2008, 08:27 PM   #2 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

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!
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
The Following User Says Thank You to RobertK For This Useful Post:
ReSpawN (01-11-2008)
Old 01-11-2008, 08:47 PM   #3 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

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.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 01-11-2008, 08:54 PM   #4 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

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.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 01-13-2008, 11:44 AM   #5 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by RobertK View Post
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!
You should wrap that into a function. bbCode(); :]
Orc is offline  
Reply With Quote
Old 01-13-2008, 09:41 PM   #6 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

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.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:01 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design