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不完全匹配。