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 Link to comment https://forums.phpfreaks.com/topic/47721-pattern-matching-question/ 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 Link to comment https://forums.phpfreaks.com/topic/47721-pattern-matching-question/#findComment-233045 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 Link to comment https://forums.phpfreaks.com/topic/47721-pattern-matching-question/#findComment-233046 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? Link to comment https://forums.phpfreaks.com/topic/47721-pattern-matching-question/#findComment-233067 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 Link to comment https://forums.phpfreaks.com/topic/47721-pattern-matching-question/#findComment-233087 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.