Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.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_Html_Hyperlink - Fatal编程技术网

Php 实现自动链接到目标网站标题功能

Php 实现自动链接到目标网站标题功能,php,html,hyperlink,Php,Html,Hyperlink,我想实现一个函数,它获取网站的目标html,并将给定链接和转换为html链接 代码: <?php function get_title($url){ $str = file_get_contents($url); if(strlen($str)>0){ $str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside <title> preg_match("/

我想实现一个函数,它获取网站的目标html,并将给定链接和
转换为html链接

代码:

<?php
function get_title($url){
  $str = file_get_contents($url);
  if(strlen($str)>0){
    $str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside <title>
    preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
    return $title[1];
  }
}
//Example:
echo get_title("http://www.washingtontimes.com/");
?>

问题: 这个过程需要时间。想象一下,有一个页面有5-20个链接

解决方案:我想转换所有URL并将它们保存为html链接,这样我的网站就不会加载太长时间

我应该这样做吗?我知道,最好将原始数据存储在数据库中,但我看不到其他解决方案


问候

这不管用吗?解析器会更安全。你的问题有点宽泛,但从表面上看还可以。您的问题是什么?当您在页面加载上运行此函数时,DOM将加载很长时间,因此我想知道,在将数据保存到数据库之前是否应该这样做!这不管用吗?解析器会更安全。你的问题有点宽泛,但从表面上看还可以。您的问题是什么?当您在页面加载上运行此函数时,DOM将加载很长时间,因此我想知道,在将数据保存到数据库之前是否应该这样做!