Quick quesiton

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

Moderator: General Moderators

Post Reply
truepal20032001
Forum Commoner
Posts: 27
Joined: Mon Jun 25, 2007 8:28 pm

Quick quesiton

Post by truepal20032001 »

how can i get the text between search words like:

<html> TEXT </html> : in this case, i want to get the content "text"
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

preg_match('!<text>(.*?)</text>!im', $source, $matches);

echo '<pre>';
print_r($matches);
I would suggest you read the stickies by Chris Corbyn in our Regex forum to get basic yet essential knowledge on regex string manipulation.

Moved to Regex.
vapoorize
Forum Newbie
Posts: 22
Joined: Mon Dec 17, 2007 5:35 pm

Re: Quick quesiton

Post by vapoorize »

truepal20032001 wrote:how can i get the text between search words like:

<html> TEXT </html> : in this case, i want to get the content "text"
!<text>(.*?)</text>!im
Looks like he said he wanted to match just the text in between not the both the text in between and the markers it sits in between:

So:

Code: Select all

~(?<=<html>).*?(?=</html>)~
*edit better yet here's an alternative with no backtracking & without a lazy ? operator

Code: Select all

~(?<=<html>)(?:(?=<html>).)*(?=</html>)~s
Post Reply