Page 1 of 1

Quick quesiton

Posted: Tue Dec 25, 2007 10:38 pm
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"

Posted: Tue Dec 25, 2007 10:42 pm
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.

Re: Quick quesiton

Posted: Mon Jan 07, 2008 7:07 pm
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