tiki Posted April 6, 2009 Share Posted April 6, 2009 So I have this BB code parsing class I wrote that works perfectly. Just one thing I cant figure out. I want all tags ([ b], etc) to be parsed EXCEPT if they are within [ code] tags. Any help would be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/152717-preg_replace-on-everything-except/ Share on other sites More sharing options...
Adam Posted April 6, 2009 Share Posted April 6, 2009 Post your parser code.. Link to comment https://forums.phpfreaks.com/topic/152717-preg_replace-on-everything-except/#findComment-802239 Share on other sites More sharing options...
tiki Posted April 6, 2009 Author Share Posted April 6, 2009 I havent even begun how to do this part, since im completely lost. Im really good at regex, just never done something like this. Im assuming it will be a preg_match_all. But heres a snippet of the code, that this needs to be applied to: private $markup = array( '/\[b\](.*?)\[\/b\]/is' => '<strong>$1</strong>', '/\[i\](.*?)\[\/i\]/is' => '<em>$1</em>', '/\[u\](.*?)\[\/u\]/is' => '<span style="text-decoration: underline">$1</span>', '/\[align=(left|center|right)\](.*?)\[\/align\]/is' => '<div style="text-align: $1">$2</div>', '/\{6}|[a-z]+)\](.*?)\[\/color\]/is' => '<span style="color: $1">$2</span>', '/\(.*?)\[\/font\]/is' => '<span style="font-family: \'$1\', sans-serif;">$2</span>', '/\[h([1-6]{1})\](.*?)\[\/h([1-6]{1})\]/is' => '<h$1>$2</h$3>', '/\{1})?[0-9]{1})\](.*?)\[\/size\]/is' => '<span style="font-size: $1px">$2</span>', '/\[code\](.*?)\[\/code\]/is' => '<pre>$1</pre>', '/\[sub\](.*?)\[\/sub\]/is' => '<sub>$1</sub>', '/\[sup\](.*?)\[\/sup\]/is' => '<sup>$1</sup>' ); // Later in the code $string = preg_replace(array_keys($this->markup), $this->markup, $string); Link to comment https://forums.phpfreaks.com/topic/152717-preg_replace-on-everything-except/#findComment-802946 Share on other sites More sharing options...
tiki Posted April 9, 2009 Author Share Posted April 9, 2009 Anyone know if this is even possible? Link to comment https://forums.phpfreaks.com/topic/152717-preg_replace-on-everything-except/#findComment-805919 Share on other sites More sharing options...
MadTechie Posted April 9, 2009 Share Posted April 9, 2009 Nothing wrong with that code my test <?php $markup = array( '/\[b\](.*?)\[\/b\]/is' => '<strong>$1</strong>', '/\[i\](.*?)\[\/i\]/is' => '<em>$1</em>', '/\[u\](.*?)\[\/u\]/is' => '<span style="text-decoration: underline">$1</span>', '/\[align=(left|center|right)\](.*?)\[\/align\]/is' => '<div style="text-align: $1">$2</div>', '/\[color=(#[0-9a-fA-F]{6}|[a-z]+)\](.*?)\[\/color\]/is' => '<span style="color: $1">$2</span>', '/\[font=\"(.*?)\"\](.*?)\[\/font\]/is' => '<span style="font-family: \'$1\', sans-serif;">$2</span>', '/\[h([1-6]{1})\](.*?)\[\/h([1-6]{1})\]/is' => '<h$1>$2</h$3>', '/\[size=((?:[1-2]{1})?[0-9]{1})\](.*?)\[\/size\]/is' => '<span style="font-size: $1px">$2</span>', '/\[code\](.*?)\[\/code\]/is' => '<pre>$1</pre>', '/\[sub\](.*?)\[\/sub\]/is' => '<sub>$1</sub>', '/\[sup\](.*?)\[\/sup\]/is' => '<sup>$1</sup>' ); $string = "this is [b]BOLD[/b][code ]so is [b]THIS[/b][/code ]"; //code without the spaces // Later in the code $string = preg_replace(array_keys($markup), $markup, $string); echo $string; ?> results Quote this is <strong>BOLD</strong><pre>so is <strong>THIS</strong></pre> Link to comment https://forums.phpfreaks.com/topic/152717-preg_replace-on-everything-except/#findComment-805932 Share on other sites More sharing options...
tiki Posted April 10, 2009 Author Share Posted April 10, 2009 Thats not the problem. For example: [ b]bold1[/ b] [ code][ b]bold2[/ b][/ code] I only want bold1 to turn bold, bold2 would not be converted at all since its within the code tags. Link to comment https://forums.phpfreaks.com/topic/152717-preg_replace-on-everything-except/#findComment-806073 Share on other sites More sharing options...
MadTechie Posted April 10, 2009 Share Posted April 10, 2009 I see.. thier is probably a few ways but heres a simple one basically i grab the string inside the [code ] tag and encode it then do the bbreplace then decode the code tag's <?php $markup = array( '/\[b\](.*?)\[\/b\]/is' => '<strong>$1</strong>', '/\[i\](.*?)\[\/i\]/is' => '<em>$1</em>', '/\[u\](.*?)\[\/u\]/is' => '<span style="text-decoration: underline">$1</span>', '/\[align=(left|center|right)\](.*?)\[\/align\]/is' => '<div style="text-align: $1">$2</div>', '/\[color=(#[0-9a-fA-F]{6}|[a-z]+)\](.*?)\[\/color\]/is' => '<span style="color: $1">$2</span>', '/\[font=\"(.*?)\"\](.*?)\[\/font\]/is' => '<span style="font-family: \'$1\', sans-serif;">$2</span>', '/\[h([1-6]{1})\](.*?)\[\/h([1-6]{1})\]/is' => '<h$1>$2</h$3>', '/\[size=((?:[1-2]{1})?[0-9]{1})\](.*?)\[\/size\]/is' => '<span style="font-size: $1px">$2</span>', '/\[sub\](.*?)\[\/sub\]/is' => '<sub>$1</sub>', '/\[sup\](.*?)\[\/sup\]/is' => '<sup>$1</sup>' ); //remove the first space from code $string = "this is [b]BOLD[/b][ code]so is [b]THIS[/b][/ code]"; //code without the spaces //remove the first space from code $string = preg_replace_callback('/\[ code\](.*?)\[\ /code\]/is',create_function('$string','return "[newcode]".base64_encode($string[1])."[/newcode]";'),$string); $string = preg_replace(array_keys($markup), $markup, $string); $string = preg_replace_callback('/\[newcode\](.*?)\[\/newcode\]/is',create_function('$string','return "<pre>".base64_decode($string[1])."</pre>";'),$string); echo $string; ?> Link to comment https://forums.phpfreaks.com/topic/152717-preg_replace-on-everything-except/#findComment-806465 Share on other sites More sharing options...
tiki Posted April 11, 2009 Author Share Posted April 11, 2009 Yeah I was thinking of doing it that way. It works perfectly, except if you place code tags within the code tags, but theres nothing we can do about that. Thanks a lot for the help! Link to comment https://forums.phpfreaks.com/topic/152717-preg_replace-on-everything-except/#findComment-807071 Share on other sites More sharing options...
laffin Posted April 11, 2009 Share Posted April 11, 2009 Actually there is but its a lot more coding. I have something like that which is approx 90% complete, but had to stall on the development of the code. wut u end up doing is parsing the bbcode tags individually and build up a stack. and when u encounter a end tag, u pop it off the stack and process the tags. nice thing, if done using a stack, is that it dusnt get confused about which are the matching end tags. FIFO stack. but it is a lot of extra coding, because you are building this stack. Than a new problem arised, wut to do with mismatched tags. Anyways good luck if u want to proceed in building a stack based bbcode parser, I can help (More like point u in right direction than providing code) Link to comment https://forums.phpfreaks.com/topic/152717-preg_replace-on-everything-except/#findComment-807076 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.