burhankhan Posted July 9, 2007 Share Posted July 9, 2007 Hi: I want to remove sub string from a string. Criteria is like this: this is starting of string [tag][tag] here is some other code[/tag]here is some code[/tag] this is ending of string... 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... I have tried many patterns but useless. Any help will be highly appreciated. Thanks Burhan Khan Quote Link to comment Share on other sites More sharing options...
effigy Posted July 9, 2007 Share Posted July 9, 2007 <pre> <?php $str = '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...'; echo preg_replace('#\[(.+?)\].+?\[/\1\]#', '', $str); ?> </pre> Quote Link to comment Share on other sites More sharing options...
burhankhan Posted July 9, 2007 Author Share Posted July 9, 2007 I have tried your script but it is not working with this string: [quote][quote]this is quote 3[/quote]this is qutoe 2[/quote]this is in the quote It should return this: this is in the quote But it is returning this: this is qutoe 2[ /quote ]this is in the quote Any suggestion? Quote Link to comment Share on other sites More sharing options...
rea|and Posted July 10, 2007 Share Posted July 10, 2007 Try this code: <?php $rex='/\[(\w+)\](?!.*?\[\\1\]).*?\[\/\\1\]/s'; while(preg_match($rex,$str,$mth)) $str=str_replace($mth[0],'',$str); ?> Quote Link to comment Share on other sites More sharing options...
burhankhan Posted July 10, 2007 Author Share Posted July 10, 2007 Thank rea|and, it is working Quote Link to comment 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.