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

是的,我正试图从网页上获取链接.链接存储在数组$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( "/\

(.*)\/", $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;

}

}

标签:php,arrays

来源: https://codeday.me/bug/20190709/1413706.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值