Page 1 of 1

Remove all between tags problem

Posted: Tue Oct 08, 2013 5:31 am
by radiks
Hi,

I need to remove all between open and close tag.
For example:
Text Text Text
<sometag>
Text1 in Tag!!!
</sometag>
Text 2 Text 2 Text 2
<sometag>
Text2 in Tag!!!
</sometag>
Text 3 Text 3 Text 3
<sometag>
Text3 in Tag!!!
</sometag>

I want to show only this as Result:
Text Text Text
Text 2 Text 2 Text 2
Text 3 Text 3 Text 3

Here is my code:
preg_replace('/<sometag>.*<\/sometag>/sm', '', $text);

But this regexp doesn't show anything.

Could you help me, please

Re: Remove all between tags problem

Posted: Tue Oct 08, 2013 10:12 am
by Christopher
Your regexp is removing everything from (and including) the first tag to the last. Maybe try preg_split() with either <sometag> or </sometag> and you will get an array of the contents. You can also use the XML parsing functions to read the contents.

Re: Remove all between tags problem

Posted: Tue Oct 08, 2013 12:05 pm
by Celauran
Or try a non-greedy match by using .*? instead of .*