2011-05-12 3 views
-1

가능한 중복 :
Grabbing the href attribute of an A element추출 URL을 사용하여 PHP

안녕하세요, 저는 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> 

내가 앵커에서 URL을 추출하고자하는이 preg_match 또는 다른 PHP 함수를 사용하는 href 속성

답변

3

Don't use regexes to parse HTML. PHP는 DOM을 사용하여 노드가 필요한 경우 처음 hasAttribute()를 사용하여 href이있는 경우

$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 />"; 

당신은 확인할 수 있습니다.

1

이 정규식을 시도

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

을 사용할 수 있습니다

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