-3

可能重复:
如何使用 PHP 解析和处理 HTML?

我得到带有 file_get_content 的页面,我想以任何方式提取页面中的所有链接来做到这一点?或者我可以使用 str 和 start 和 end phares 来获取这样的目标字符串:

$str=fdgdfbfbmnlmnjkl njnkhvnbn j<a href="http://www.google.com">google</a>
$link=str($str,"start","END")??????????
EX : $link=str($str,"http://www","com")=Res=>http://www.google.com or google?

或者

$str=file_get_content("http://www.google.com");
    $link=str($str,"start","END")??????????
    EX : $link=str($str,"http://www","com")=Res=>http://www.google.com or google?
4

2 回答 2

1

前段时间我遇到了同样的问题。这个解决方案对我来说效果很好。

 $string = "Hello World, <a href='http://www.google.com'>Google</a> ! Search also on <a href='http://www.bing.com'>Bing</a>";

 preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $string, $match);

 $matches = $match[0];

 foreach($matches as $var)
 {    
     print($var."<br>"); 
 }
于 2012-05-28T08:55:33.777 回答
0

您应该使用 DOM 方法从 HTML 中提取内容 - 使用正则表达式会导致疯狂

<?php
    $dom = new DOMDocument;
    $dom->loadHTMLFile('http://www.google.com/');

    $a = $dom->getElementsByTagName('a');
    foreach ($a as $e) {
        echo $e->getAttribute("href") . "\n";
    }
?>
于 2012-05-28T08:52:55.860 回答