homepage Welcome to WebmasterWorld Guest from 54.196.189.229
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Need regEx to extract string between anchor tags in PHP
preg-match, PHP
iamvela




msg:3698686
 9:14 am on Jul 15, 2008 (gmt 0)

I am trying to extract text between
Reply to: <a ..............>*</a>
i.e. I want the value of *


I tried
if (0 < preg_match("/^Reply to: \<a.\>(.)+</a>/", $page, $matches))
{
echo $matches[1] . "<br>" ;
echo $matches[2] . "<br>" ;
}
else
echo "Pattern not matched <br>" ;

and this
if (0 < preg_match("<a\b[^>]*>(.*?)</a>", $page, $matches))
{
echo $matches[1] . "<br>" ;
echo $matches[2] . "<br>" ;
}
else
echo "Pattern not matched <br>" ;

neither worked.

I'd really appreciate any help, THANKS!

 

adb64




msg:3698706
 9:37 am on Jul 15, 2008 (gmt 0)

Try: preg_match [php.net]("#Reply to: <a.+>(.+)</a>#U",$page,$matches)
The U modifier makes it ungreedy and using a # as delimiter instead of / which clashes with the / in </a>
There are several online regex testers available, Google for 'online regular expression tester' and you will find several.

iamvela




msg:3699066
 5:52 pm on Jul 15, 2008 (gmt 0)

Thanks sooooooooooo much!

I did try testers before posting this, but am weak on the writing regex. As you can see I was very close, but as they say "close, but no cigar" :)

Again thanks a bunch for your help!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved