脚本需要 30 秒以上才能在 PHP 中获取标题 URL


script taking more than 30 second to get title url in php

<?php
function getTitle($Url){
    $str = file_get_contents($Url);
    if(strlen($str)>0){
        preg_match("/'<title'>(.*)'<'/title'>/",$str,$title);
        return $title[1];
    }
}
echo getTitle("http://www.stackoverflow.com/");
?>

当我运行此脚本时,它返回错误

max_execution_time exceeds 30 second.

我不想增加max_execution_time但我想减少脚本运行时间。

您正在收集整个页面。为什么不只指定$maxlen参数,并或多或少地获取标题标签所在的页面的标题?这会给你一点加速。

string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )