4

对,我正在尝试从网页中获取链接。链接存储在数组 $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;
4

1 回答 1

1

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

$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;
    }
} 
于 2012-09-17T01:12:22.363 回答