-1

Possible Duplicate:
Grabbing the href attribute of an A element

hi, i have this string in PHP

<iframe frameborder="0" width="320" height="179" src="http://www.dailymotion.com/embed/video/xinpy5?width=320&wmode=transparent"></iframe><br /><a href="http://www.dailymotion.com/video/xinpy5_le-buzz-pippa-middleton-agace-la-reine_news" target="_blank">Le buzz Pippa Middleton agace la Reine !</a> <i>par <a href="http://www.dailymotion.com/direct8" target="_blank">direct8</a></i>

i would like to extract the url from the anchor href attribute using preg_match or other php functins

Community
  • 1
  • 1
NoOneElse
  • 1,101
  • 2
  • 11
  • 15

3 Answers3

3

Don't use regexes to parse HTML. Use the PHP DOM:

$DOM = new DOMDocument;
$DOM->loadHTML($str); // Your string

//get all anchors
$anchors = $DOM->getElementsByTagName('a');

//display all hrefs
for ($i = 0; $i < $anchors->length; $i++)
    echo $anchors->item($i)->getAttribute('href') . "<br />";

You can check if the node has a href using hasAttribute() first if necessary.

Community
  • 1
  • 1
Håvard S
  • 23,244
  • 8
  • 61
  • 72
1

You can use

if (preg_match('#<a\s*[^>]*href="([^"]+)"#i', $string, $matches))
echo $matches[0];
Sebastian Schmidt
  • 1,078
  • 7
  • 17
0

try this regex

(?<=href=\")[\w://\.\-]+
AabinGunz
  • 12,109
  • 54
  • 146
  • 218