加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 从这个数组中获取标题

发布时间:2020-12-13 22:45:06 所属栏目:PHP教程 来源:网络整理
导读:是的,我正试图从网页上获取链接.链接存储在数组$links中. 有没有办法让我从数组中的每个链接中获取标题(来自下面的函数).那将是一个多维数组吗?我怎样才能做到这一点? $links = Array();$URL = 'http://www.theqlick.com'; // change it for urls to grab
是的,我正试图从网页上获取链接.链接存储在数组$links中.

有没有办法让我从数组中的每个链接中获取标题(来自下面的函数).那将是一个多维数组吗?我怎样才能做到这一点?

$links = Array();
$URL = 'http://www.theqlick.com'; // change it for urls to grab  
// grabs the urls from URL 
$file  = file_get_html($URL);
foreach ($file->find('a') as $theelement) {
   $links[] = url_to_absolute($URL,$theelement->href);
} 
print_r($links);


    function getTitle($links) 
    {
      //change it for the original titles. 
      $str = file_get_contents("http://www.theqlick.com"); 
      if ( strlen( $str )>0 ) {
        preg_match( "/&;title&;(.*)&;/title&;/",$str,$title );
     return $title[1];
    }
 } 

 $metatitle = getTitle();
 echo $metatitle;

解决方法

我没有安装正确的库来测试它,但它应该让你知道从哪里开始:

$links = Array();
$URL = 'http://www.theqlick.com'; // change it for urls to grab  
// grabs the urls from URL 
$file  = file_get_html($URL);
foreach ($file->find('a') as $theelement) {
    $link = array();
    $link['url'] = url_to_absolute($URL,$theelement->href);
    $link['title'] = getTitle($link['url']);
    $links[] = $link;
} 
print_r($links);

function getTitle($url) 
{
    $file = file_get_html($url);
    $titles = $file->find('title');
    if (is_array($titles)) {
        return $titles[0];
    } else {
        return null;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读