Jump to content

Regex Simple question


techcone

Recommended Posts

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.  :D

Link to comment
https://forums.phpfreaks.com/topic/134880-regex-simple-question/
Share on other sites

<?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>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.