php - 如何使用phpquery从数组中的标签中获取属性、值和文本
问题描述
我正在尝试使用 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);
?>
解决方案
如果您print_r($doc)
在以下行之后,您是否看到了您期望看到的文档结构?
$doc = $doc['body']->find('main')->find('.artfeed')->find('.hl');
我以前使用过简单的 HTML Dom,但没有使用过 phpQuery,所以我不确定上面的行或其他地方是否有错误。
根据我看到的示例,您应该能够使用 CSS 语法来查找元素。将您的文档更改为以下内容:
$doc = $doc['body']->find('main')->find('.artfeed');
然后只需使用带有 CSS 语法的 pq() 和 find() 即可直接查找元素而无需循环。
$content = pq($doc);
$links[] = array(
$content->find('div.hl > span.f')->attr('country'),
$content->find('div.hl > div.hl__inner > a.hll')->attr('href'),
$content->find('div.hl > div.hl__inner > a.hll')->text(),
);
编辑:对于多个 hl div,我认为这样的事情可能有效:
$doc = $doc['body']->find('main')->find('.artfeed');
foreach (pq($doc)->find('.hl') as $hl) {
$links[] = array(
$hl->find('span.f')->attr('country'),
$hl->find('div.hl__inner > a.hll')->attr('href'),
$hl->find('div.hl__inner > a.hll')->text(),
}
);
推荐阅读
- sml - 为什么我使用 `let` 而不仅仅是 `val` 在 SML 中的函数内声明变量?
- c# - 尝试将 FK 插入表中而无需用户在页面 ASP.NET / C# 上手动输入 FK
- php - MySql 查询使用 2 个提交按钮表单返回空白页
- postgresql - Postgres_FDW 没有推低 WHERE 标准
- object - 有没有办法在 GoLang 中列出对象的方法?
- java - Jni - 使用 void 指针从 java 调用 c++ 方法
- html - 如何使用 nth-child 属性更改背景图像?
- r - 减少刻度和 xy 轴之间的距离@ggplot
- d3.js - 创建表时出现 D3 问题
- scala - 为什么我需要一个额外的 asInstanceOf