Remove all between tags problem

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
radiks
Forum Newbie
Posts: 9
Joined: Thu Aug 22, 2013 4:12 am

Remove all between tags problem

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Remove all between tags problem

Post 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.
(#10850)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Remove all between tags problem

Post by Celauran »

Or try a non-greedy match by using .*? instead of .*
Post Reply