PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Jul 22, 2018 11:37 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Oct 08, 2013 5:31 am 
Offline
Forum Newbie

Joined: Thu Aug 22, 2013 4:12 am
Posts: 9
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


Top
 Profile  
 
PostPosted: Tue Oct 08, 2013 10:12 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13570
Location: New York, NY, US
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)


Top
 Profile  
 
PostPosted: Tue Oct 08, 2013 12:05 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6424
Location: Montreal, Canada
Or try a non-greedy match by using .*? instead of .*

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group