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 Link to comment https://forums.phpfreaks.com/topic/59142-solved-substring-using-preg_replace/ 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> Link to comment https://forums.phpfreaks.com/topic/59142-solved-substring-using-preg_replace/#findComment-293690 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? Link to comment https://forums.phpfreaks.com/topic/59142-solved-substring-using-preg_replace/#findComment-293852 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); ?> Link to comment https://forums.phpfreaks.com/topic/59142-solved-substring-using-preg_replace/#findComment-294142 Share on other sites More sharing options...
burhankhan Posted July 10, 2007 Author Share Posted July 10, 2007 Thank rea|and, it is working Link to comment https://forums.phpfreaks.com/topic/59142-solved-substring-using-preg_replace/#findComment-294169 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.