Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 抓取页面中的所有链接_Php_Regex_Url_Preg Match All - Fatal编程技术网

Php 抓取页面中的所有链接

Php 抓取页面中的所有链接,php,regex,url,preg-match-all,Php,Regex,Url,Preg Match All,我想从页面中获取所有链接(href) 这是我的实际代码: preg_match_all('/href=.([^"\' ]+)/i', $content, $anchor); 但它只抓取域和子域(如name.name.ex或name.ex),而不抓取自定义URL,如name.ex/name/name.php 有人能帮我使用正则表达式吗?试试这个正则表达式: $pattern = "/href="([^\s"]+)/"; preg_match_all($pattern, $content, $ma

我想从页面中获取所有链接(href)

这是我的实际代码:

preg_match_all('/href=.([^"\' ]+)/i', $content, $anchor);
但它只抓取域和子域(如
name.name.ex
name.ex
),而不抓取自定义URL,如
name.ex/name/name.php


有人能帮我使用正则表达式吗?

试试这个正则表达式:

$pattern = "/href="([^\s"]+)/";
preg_match_all($pattern, $content, $matches);

if (count($matches[1]) {
  foreach($matches[1] as $match)
    echo $match . "<br />";
}
$pattern=“/href=“([^\s”]+)/”;
preg_match_all($pattern、$content、$matches);
如果(计数($matches[1]){
foreach($matches[1]作为$match)
echo$match。“
”; }
试试这个正则表达式:

$pattern = "/href="([^\s"]+)/";
preg_match_all($pattern, $content, $matches);

if (count($matches[1]) {
  foreach($matches[1] as $match)
    echo $match . "<br />";
}
$pattern=“/href=“([^\s”]+)/”;
preg_match_all($pattern、$content、$matches);
如果(计数($matches[1]){
foreach($matches[1]作为$match)
echo$match。“
”; }
给你

$string = "<a href='test.php/url' class=>test</a>testar <a href='test2.php/url2' class=>test</a>";
$pattern = "/<a(?:[^>]*)href=([^ ]*)(?:[^>]*)>/";

preg_match_all($pattern, $string, $matches);

foreach($matches[1] as $match){
    echo $match;
}
$string=“testar”;
$pattern=“/]*)href=([^]*)(?:[^>]*)>/;
preg_match_all($pattern,$string,$matches);
foreach($matches[1]作为$match){
回声$匹配;
}
给你

$string = "<a href='test.php/url' class=>test</a>testar <a href='test2.php/url2' class=>test</a>";
$pattern = "/<a(?:[^>]*)href=([^ ]*)(?:[^>]*)>/";

preg_match_all($pattern, $string, $matches);

foreach($matches[1] as $match){
    echo $match;
}
$string=“testar”;
$pattern=“/]*)href=([^]*)(?:[^>]*)>/;
preg_match_all($pattern,$string,$matches);
foreach($matches[1]作为$match){
回声$匹配;
}

我建议不要为此使用正则表达式。我建议您使用来解析并获得结果

下面是使用
DOM
XPath

$html = '<a href="name.ex/name/name.php">text</a>
         <a href="foo.com">foobar</a>';

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

$xpath = new DOMXPath($doc);

foreach ($xpath->query('//a') as $link) {
   $links[] = $link->getAttribute('href');
}

print_r($links);
$html='1!'
';
$doc=新的DOMDocument();
$doc->loadHTML($html);
$xpath=新的DOMXPath($doc);
foreach($xpath->query('//a')作为$link){
$links[]=$link->getAttribute('href');
}
打印(链接);

请参见

我建议不要为此使用正则表达式。我建议您使用来解析并获得结果

下面是使用
DOM
XPath

$html = '<a href="name.ex/name/name.php">text</a>
         <a href="foo.com">foobar</a>';

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

$xpath = new DOMXPath($doc);

foreach ($xpath->query('//a') as $link) {
   $links[] = $link->getAttribute('href');
}

print_r($links);
$html='1!'
';
$doc=新的DOMDocument();
$doc->loadHTML($html);
$xpath=新的DOMXPath($doc);
foreach($xpath->query('//a')作为$link){
$links[]=$link->getAttribute('href');
}
打印(链接);

请参见

更易于使用的文档:

$doc = new DOMDocument();
@$doc->loadHTML($html);

$linkNodes = $doc->getElementsByTagName('a');

foreach($linkNodes as $linkNode) {
    $urls[] = $linkNode->getAttribute('href');
}

print_r($urls);

使用DOMDocument更容易:

$doc = new DOMDocument();
@$doc->loadHTML($html);

$linkNodes = $doc->getElementsByTagName('a');

foreach($linkNodes as $linkNode) {
    $urls[] = $linkNode->getAttribute('href');
}

print_r($urls);

您可以列出所有域(即.com、.org、.net等),然后对所有域进行预匹配。这里是所有TLD的wiki,您可以列出所有域(即.com、.org、.net等),然后对所有域进行预匹配。这是所有TLD的维基