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

该博客讨论了如何从网页上的链接数组中获取每个链接的标题。通过使用PHP的file_get_html函数解析HTML,然后遍历'a'标签,将链接和其对应的标题存储到一个多维数组中。每个数组元素包含链接URL和对应的页面标题。提供的解决方案还包含一个getTitle函数,用于从给定URL获取页面标题。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值