Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.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,我在PHP中使用这个函数来获取页面标题。我知道这看起来有点混乱,但那是因为我是PHP的初学者。我以前在if中使用过preg_match(“/(.+)/I)”,$returned_content,$m),但它没有像我预期的那样工作 function get_page_title($url) { $returned_content = get_url_contents($url); $returned_content = str_replace("\n", "", $returned

我在PHP中使用这个函数来获取页面标题。我知道这看起来有点混乱,但那是因为我是PHP的初学者。我以前在if中使用过
preg_match(“/(.+)/I)”,$returned_content,$m)
,但它没有像我预期的那样工作

function get_page_title($url) {
    $returned_content = get_url_contents($url);
    $returned_content = str_replace("\n", "", $returned_content);
    $returned_content = str_replace("\r", "", $returned_content);
    $lower_rc = strtolower($returned_content);
    $pos1 = strpos($lower_rc, "<title>") + strlen("<title>");
    $pos2 = strpos($lower_rc, "</title>");
    if ($pos2 > $pos1)
        return substr($returned_content, $pos1, $pos2-$pos1);
    else
        return $url;
}
函数获取页面标题($url){
$returned\u content=get\u url\u contents($url);
$returned\u content=str\u replace(“\n”,”,$returned\u content);
$returned\u content=str\u replace(“\r”,”,$returned\u content);
$lower\u rc=strtolower($returned\u content);
$pos1=strpos($lower_rc,“”)+strlen(“”);
$pos2=STRPO($lower_rc,“”);
如果($pos2>$pos1)
return substr($returned_content,$pos1,$pos2-$pos1);
其他的
返回$url;
}
这是我尝试使用上述函数获取以下页面标题时得到的结果: ->“302已移动” -> ""http://www.facebook.com" -> "http://www.revistabula.com/posts/listas/100-links-para-clicar-antes-de-morrer" (当我在链接末尾添加一个/时,我可以成功地获得标题:“100个链接(100个链接)para clicar antes de morrer | Revista Bula”)

我的问题是: -当我尝试访问google.com时,我知道google正在重定向到我国家的镜像,但我如何才能获得它重定向到的页面的标题?
-我的函数中有什么错误使得它获取某些页面的标题,而不是其他页面的标题?

HTTP客户端应该遵循重定向。302状态代码意味着您尝试获取的内容不在该位置,客户端应该遵循
位置:
标题来确定它在哪里

这里有两个问题。第一个问题是不遵循重定向。如果使用cURL,可以通过设置以下选项使其遵循重定向:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
有关完整解决方案,请参见此问题:

第二个问题是您正在使用正则表达式解析HTML。有关更好的替代方法,请参见此问题:


为什么不试试这样的东西?效果很好

function get_page_title($url) 
{
        $source = file_get_contents($url);

        $results = preg_match("/<title>(.*)<\/title>/", $source, $title_matches);
        if (!$results) 
            return null; 

        //get the first match, this is the title 
        $title = $title_matches[1];
        return $title;
}
函数获取页面标题($url)
{
$source=文件内容($url);
$results=preg_match(“/(.*)/”,$source,$title_matches);
如果(!$results)
返回null;
//获得第一场比赛,这是冠军
$title=$title_匹配[1];
返回$title;
}

获取url内容($url)返回什么?获取url内容()的代码是什么?我已经接受了答案。
获取url内容()
返回页面html代码。使用正则表达式解析html?这当然是可能的,我发现它在某些情况下工作得很好。我使用PHP多年,不知道如何使用DOM解析器。对于初学者来说,使用正则表达式解析html似乎是一个很好的选择。你是对的:使用正则表达式解析html在某些情况下可以工作。但是,好的代码应该处理所有有效的输入,当HTML是输入时,正则表达式不能处理所有有效的输入。您的正则表达式将无法在这个丑陋但完全有效的HTML片段上返回预期结果:
Site X]>
。完美答案!在您传递的链接中,“用PHP解析HTML的最佳方法”,我发现“简单的HTML Dom解析器”,它解决了我的问题。非常好。很高兴我能帮上忙。