denhamd2 Posted April 19, 2007 Share Posted April 19, 2007 Hi, I have HTML stored in a variable $html Buried in there is a link with the anchor text as New York therefore in the $html variable somewhere is <a href="somelink">New York</a> Is there any way using pattern matching that would let me get the link used to link to the text New York? There is a few links with the text New York but I only want to get the link from the first one. Been agonising over this for ages so if anyone could help it would really save my life. Thanks in advance Quote Link to comment Share on other sites More sharing options...
monk.e.boy Posted April 19, 2007 Share Posted April 19, 2007 if (preg_match('/<a\\shref="([^"]*)"[^>]*>New\\sYork<\/a>/', $subject, $regs)) { $result = $regs[1]; } else { $result = ""; } This will return "somelink" I say, http://www.regexbuddy.com/ is fun fun fun. monk.e.boy Quote Link to comment Share on other sites More sharing options...
monk.e.boy Posted April 19, 2007 Share Posted April 19, 2007 Oh, I just realised that a link: <a href=poop.html>New York</a> is valid html but won't match oops. You'll have to figure that out. monk.e.boy Quote Link to comment Share on other sites More sharing options...
denhamd2 Posted April 19, 2007 Author Share Posted April 19, 2007 that doesn't seem to work any ideas? Quote Link to comment Share on other sites More sharing options...
monk.e.boy Posted April 19, 2007 Share Posted April 19, 2007 post your code. Are you passing in $html? as $subject? monk.e.boy Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.