Jump to content

[SOLVED] Substring using preg_replace


burhankhan

Recommended Posts

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
Share on other sites

<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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.