techcone Posted November 30, 2008 Share Posted November 30, 2008 Hello coders, a simple question from me as I am n00b in regex. I have ample urls in this format . <a href="http://www.abc.com/1.html"> text 1 </a> <a href="http://www.abc.com/2.html"> text 2 </a> I want to extract text and its url like : http://www.abc.com/1.html - text 1 http://www.abc.com/2.html - text 2 Can anyone provide me the regular expression for it. Thanx in advance. Link to comment https://forums.phpfreaks.com/topic/134880-regex-simple-question/ Share on other sites More sharing options...
rhodesa Posted November 30, 2008 Share Posted November 30, 2008 <?php $html = ' <a href="http://www.abc.com/1.html"> text 1 </a> <a href="http://www.abc.com/2.html"> text 2 </a> '; preg_match_all('/<a href="(.*?)">(.*?)<\/a>/',$html,$matches); foreach(array_keys($matches[0]) as $n){ $url = $matches[1][$n]; $text = $matches[2][$n]; print "$url - $text<br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/134880-regex-simple-question/#findComment-702307 Share on other sites More sharing options...
techcone Posted November 30, 2008 Author Share Posted November 30, 2008 Wow thanks rhodesa . Will return back to you for more help Link to comment https://forums.phpfreaks.com/topic/134880-regex-simple-question/#findComment-702310 Share on other sites More sharing options...
techcone Posted November 30, 2008 Author Share Posted November 30, 2008 One more simple query some time the url is like this : <a href="http://www.abc.com/1.html"> text 1 </a> How to catch these in nets ? Link to comment https://forums.phpfreaks.com/topic/134880-regex-simple-question/#findComment-702320 Share on other sites More sharing options...
rhodesa Posted November 30, 2008 Share Posted November 30, 2008 add an s modifier to the regex: preg_match_all('/<a href="(.*?)">(.*?)<\/a>/s',$html,$matches); Link to comment https://forums.phpfreaks.com/topic/134880-regex-simple-question/#findComment-702322 Share on other sites More sharing options...
techcone Posted November 30, 2008 Author Share Posted November 30, 2008 Was to to post the soln only Found some where else. Link to comment https://forums.phpfreaks.com/topic/134880-regex-simple-question/#findComment-702323 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.