Php 解析外部网页并从内容中提取所有URL和链接文本

Php 解析外部网页并从内容中提取所有URL和链接文本,php,string,Php,String,我想解析外部网页,并提取所有的URL和链接文本的内容使用PHP 比如说, $content="<a href="http://google.com" target="_blank"> google</a> is very good search engine <a href="http://gmail.com" target="_blank">Gmail </a> is provided by google. 非常感谢您的建议 您可以使用此正则表

我想解析外部网页,并提取所有的URL和链接文本的内容使用PHP

比如说,

$content="<a href="http://google.com" target="_blank"> google</a> is very good search engine <a href="http://gmail.com" target="_blank">Gmail </a> is provided by google.

非常感谢您的建议

您可以使用此正则表达式模式
href=“([a-zA-Z0-9://.]+)”

示例用法

$pattern = 'href="([a-zA-Z0-9://. ]+)"';
$content = file_get_contents(FILE NAME HERE);
preg_match($pattern, $content, $matches);

print_r($matches);

这将列出所有链接。然后您可以解析它们。

如果您想使用正则表达式提取url和文本,那么以下操作应该可以:

<\s*a\s*href\s*=\"(?<url>.*)\">(?<text>.*)</a>

DOM如何使用php编写程序。我对它没有任何概念DOM@user1032289编辑了答案。谢谢,这对我很有用
<\s*a\s*href\s*=\"(?<url>.*)\">(?<text>.*)</a>
$content = "< a href="http://google.com" target="_blank"> google</a> is very good search engine < a href="http://gmail.com" target="_blank">Gmail </a> is provided by google .";

$html = new DOMDocument();
$html->loadHTML($content);

$anchors = $html->getElementsByTagName('a');
foreach ($anchors as $anchor) {
       echo $anchor->getAttribute('href') . "\t" . $anchor->nodeValue;
}