dongxing2263 2013-08-06 09:05
浏览 33
已采纳

过滤子标签值

How to filter child tags value of a <div> and display just the parent inner texts? For example in this code :

include('simple_html_dom.php');

$htm='<div class="date">
         <span class="title">post date <!-- or Anything --> :</span>
         2103/04/07 13:06
      </div>';

$html = str_get_html($htm);    

$date = $html->find('.date ',0)->plaintext;

echo $date;

The result is :

post date : 2103/04/07 13:06 

But I need :

2103/04/07 13:06  

Is there any way to filter the <span> values? I prefer to not use patterns in my case. Thank you

  • 写回答

1条回答 默认 最新

  • duanbing2963 2013-08-06 09:18
    关注

    This is possibly not the cleanest solution, but it works:

    $dateDiv = $html->find('.date', 0);
    $textElems = $dateDiv->find('text');
    
    $str = '';
    foreach ($textElems as $subText) {
        if ($subText->parent() === $dateDiv) {
            $str .= $subText->plaintext;
        }
    }
    echo $str;
    

    It retrieves all text blocks and then removes all nodes which are not direct children of <div class="date">.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效