Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.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 - Fatal编程技术网

Php 获取网页问题的标题

Php 获取网页问题的标题,php,Php,我想获取包含文件\u get\u contents()的网页标题 我试过: $get=file_get_meta_tags("http://example.com"); echo $get["title"]; 但它不匹配 它有什么问题?Title标记不是get\u meta\u tags()函数中匹配的一部分,它也不是meta标记 试试这个: $get=file_get_contents("http://example.com"); preg_match("#<title>(.*?

我想获取包含
文件\u get\u contents()
的网页标题

我试过:

$get=file_get_meta_tags("http://example.com");
echo $get["title"];
但它不匹配


它有什么问题?

Title标记不是
get\u meta\u tags()
函数中匹配的一部分,它也不是meta标记

试试这个:

$get=file_get_contents("http://example.com");
preg_match("#<title>(.*?)</title>#i,$get,$matches);
print_r($matches);
$get=file\u get\u内容(“http://example.com");
preg#u match(“#(.*)#i,$get,$matches);
打印(匹配项);

Regex
#(.*)#i
与标题字符串匹配。

使用下面的代码片段获取网页标题

<?php
    function curl_file_get_contents($url)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }
    $targetUrl = "http://google.com/";
    $html = curl_file_get_contents($targetUrl);

    $doc = new DOMDocument();
    @$doc->loadHTML($html);
    $nodes = $doc->getElementsByTagName('title');

    $page_title = $nodes->item(0)->nodeValue;

    echo "Title: $page_title". '<br/><br/>';
?>


因为
不是一个
标记。
文件获取内容()
!=
文件获取元标记()
使用DOM解析器,而不是regexp。@Barmar我为什么要使用DOM解析器,因为使用regexp要容易得多?因为HTML可能与您的regexp不完全匹配。