Hi: I have written a class to parse bbcode. I am having little problem with regular expression. preg_replace('/\[quote\](.+?)\[\/quote\]/is', '<pre>$1</pre>', $mytext) Code (markup): This is simple code to convert all QUOTES tags. It is working fine. But when i have text like this: [ quote ]here is some text[ quote ]here is more text[ / quote ][ / quote ] Code (markup): Then it is creating problem. It will print this: [ quote ]here is some text [ / quote ] Code (markup): Is there any solution to parse 2 or more level QUOTES?? Thanks Burhan Khan
Try this. preg_replace('@\[(?i)quote\](.*?)\[/(?i)quote\]@si', '<pre>$1</pre>', $mytext) Code (markup):
Judging by the previous replies, I think the spaces were just to stop the forum parsing the quote tags. The problem is not with the regex, it's the nested bbcode - without a much more complex parser, you can't match up the corresponding open and close tags. After parsing a quote tag, it continues from the end position of the last replacement to look for the next opening quote tag. Obviously that won't work if we have a nested quote tag because we need it to go back to the beginning in order to find it. As already mentioned, you would need a more advanced parser to match up the open quote tags with their respective close tags. However, if we just put the replacement in a loop, it will continue finding pairs of tags till everything has been converted. I dunno if that makes sense, hopefully should do with an example. (I'll rename quote to "tag" so I can use the [tag] syntax without the forum bbcode parser interfering). Using the example text in the OP, the first replacement would convert: [b][tag]here is some text[tag]here is more text[/tag][/b][/tag] Code (markup): Now if we rerun the replacement, it will next work on: <tag>here is some text[b][tag]here is more text</tag>[/tag][/b] Code (markup): And that gets converted to our desired format: <tag>here is some text<tag>here is more text</tag></tag> Code (markup): The parser never actually works out how much it should consume to match up the pairs of [tag] and [/tag] correctly, but by continuing the replacements till all pairs are converted (albeit mismatched pairs), we can get the desired end result. while ( preg_match('/\[quote\](.+?)\[\/quote\]/is', $mytext) ) $mytext = preg_replace('/\[quote\](.+?)\[\/quote\]/is', '<pre>$1</pre>', $mytext); PHP:
Thanks rodney88 , it is working for me now! But i have little more problem. Actually i want to remove sub string from a string. Criteria is like this: this is starting of string [B][tag][tag] here is some other code[/tag]here is some code[/tag][/B] this is ending of string... Code (markup): You can see in this string there are many nested [tags]. I just want to remove those all nested tags. And out put should be: this is starting of string this is ending of string... Code (markup): I have tried many patterns but useless. Any help will be highly appreciated. Thanks Burhan Khan