【发布时间】:2020-10-11 23:27:34
【问题描述】:
我正在尝试使用 PHPQuery 从一个大文件中获取值、文本和超链接,并将其转换为数组。
我已经尝试了一些代码,但在 foreach 循环中将数据从所有 class="hl" 获取到一个数组中时感到困惑。
<?php
$str ='
<main>
<div class="artfeed ">
<div class="split split_0">
<div class="split_in">
<div class="hl" data-id="1036294107">
<span class="f" country="US"><!-- --></span>
<div class="hl__inner"><a class="hll" href="http://example.com/001/" target="_blank" rel="nofollow">Some of text here</a>
<span class="end"></span>
<span class="meta">
<span class="src" data-pub="DATAPUB">
<span class="src-part">
exampleOne.com
<svg class="svg-inline--fa fa-cog fa-w-16" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="cog" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg="">
</span>
</span>
<span class="time" data-time="1592802284">12:04</span>
</span>
<a class="hl__menu-toggle c-context-menu__btn js-article-menu__toggle" href="#"></a>
</div>
</div>
<div class="hl" data-id="1036294107">
<span class="f" country="US"><!-- --></span>
<div class="hl__inner"><a class="hll" href="http://example.com/001/" target="_blank" rel="nofollow">Some of text here</a>
<span class="end"></span>
<span class="meta">
<span class="src" data-pub="DATAPUB">
<span class="src-part">
exampleOne.com
<svg class="svg-inline--fa fa-cog fa-w-16" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="cog" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg="">
</span>
</span>
<span class="time" data-time="1592802284">12:04</span>
</span>
<a class="hl__menu-toggle c-context-menu__btn js-article-menu__toggle" href="#"></a>
</div>
</div>
<div class="hl" data-id="1036294107">
<span class="f" country="US"><!-- --></span>
<div class="hl__inner"><a class="hll" href="http://example.com/001/" target="_blank" rel="nofollow">Some of text here</a>
<span class="end"></span>
<span class="meta">
<span class="src" data-pub="DATAPUB">
<span class="src-part">
exampleOne.com
<svg class="svg-inline--fa fa-cog fa-w-16" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="cog" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg="">
</span>
</span>
<span class="time" data-time="1592802284">12:04</span>
</span>
<a class="hl__menu-toggle c-context-menu__btn js-article-menu__toggle" href="#"></a>
</div>
</div>
</div>
</div>
</div>
</main>
';
?>
需要这样的结果:
/*
Array()
Need result:
Country : US
href : http://example.com/001/
Text : Some of text here
src-part : exampleOne.com
time : 12:04
Country : US
href : http://example.com/001/
Text : Some of text here
src-part : exampleOne.com
time : 12:04
Country : US
href : http://example.com/001/
Text : Some of text here
src-part : exampleOne.com
time : 12:04
*/
我有一些代码
<?php
require("phpQuery.php");
$doc = phpQuery::newDocument($str);
$doc = $doc['body']->find('main')->find('.artfeed')->find('.hl');
$links = array();
foreach($doc['div'] as $item)
{
$node = pq($item);
$sibling = $node->next();
if ( $sibling->is('a:first') ) {
$links[] = array(
$node->attr('country'),
$sibling->attr('href'),
$sibling->text(),
);
}
}
// Display result:
print_r($links);
?>
【问题讨论】:
-
$links 的当前输出是什么?我立即注意到的一件事是国家/地区设置在跨度上,而不是 div 上,所以我认为这样获取 attr 行不通。
-
当前输出为空数组,能否在 foreach 循环中提及 class="hl" 类?