繁体   English   中英

如何使用phpquery从数组中的标签中获取属性、值和文本

[英]How to get attribute, value and text from tags in array using 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');

我之前使用过 Simple HTML Dom 但不是 phpQuery 所以我不确定上面的行或其他地方是否有错误。

根据我看到的示例,您应该能够使用 CSS 语法来查找元素。 将您的文档更改为以下内容:

$doc =  $doc['body']->find('main')->find('.artfeed');

然后只需将 pq() 和 find() 与 CSS 语法一起使用,即可直接查找元素而无需循环。

$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(),
    }
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM