-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

0

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use

if (preg_match('#<a\s*[^>]*href="([^"]+)"#i', $string, $matches))
echo $matches[0];

Comments

0

try this regex

(?<=href=\")[\w://\.\-]+

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.