duanli3277 2011-12-12 11:33
浏览 68
已采纳

选择所有子项文本值[重复]

This question already has an answer here:

Text to parse

<div id="test">some<b>bold</b> or <i>italic</i> text</div>
<div id="test">and again<b> bold text</b><i>and italic text<i></div>

Result i'd like to have

1 : some bold or italic text
2 : and again blod text and italic text

What I tried

string(//div)
normalize-space(//div)

Give the good formatting answer, but only one result came.

id('test')//text()

Give all text but split the result.

I tried to use string-join, or concat but with no luck. I want to do this in php.

</div>
  • 写回答

4条回答 默认 最新

  • douduan7295 2011-12-12 12:27
    关注

    Try this:

                 $dom = new \DOMDocument();
                 $dom->loadHTML('<!DOCTYPE HTML>
    <html lang="en-US">
    <head>
           <meta charset="UTF-8">
           <title></title>
    </head>
    <body>
           <div id="test1">some<b>bold</b> or <i>italic</i> text</div>
           <div id="test2">and again<b> bold text</b><i>and italic text</i></div>
    </body>
    </html>');
    
                  $xpath = new \DOMXPath($dom);
                  foreach ( $xpath->query('//div[contains(@id,"test")]') as $node ) {
                          echo $node->nodeValue , PHP_EOL;
                  }
    

    Outputs:

    somebold or italic text
    and again bold textand italic text
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀