php 中 h2,解析HTML并获取所有h3之后的h2之前的下一个h2使用PHP

我期待在文章中找到第一个h2.一旦找到,找到所有h3,直到找到下一个h2.冲洗并重复,直到找到所有标题和副标题.

在您立即将此问题标记或关闭为重复解析问题之前,请注意问题标题,因为这与基本节点检索无关.我已经把那部分搞定了.

我的代码如下:

$matches = array();

$dom = new DOMDocument;

$dom->loadHTML($content);

foreach($dom->getElementsByTagName('h2') as $node) {

$matches['heading-two'][] = $dom->saveHtml($node);

}

foreach($dom->getElementsByTagName('h3') as $node) {

$matches['heading-three'][] = $dom->saveHtml($node);

}

if($matches){

$this->key_points = $matches;

}

这给了我一个类似的输出:

array(

'heading-two' => array(

'

Here is the first heading two

',

'

Here is the SECOND heading two

'

),

'heading-three' => array(

'

Here is the first h3

',

'

Here is the second h3

',

'

Here is the third h3

',

'

Here is the fourth h3

',

)

);

我希望有更多的东西:

array(

'

Here is the first heading two

' => array(

'

Here is an h3 under the first h2

',

'

Here is another h3 found under first h2, but after the first h3

'

),

'

Here is the SECOND heading two

' => array(

'

Here is an h3 under the SECOND h2

',

'

Here is another h3 found under SECOND h2, but after the first h3

'

)

);

我并不是在寻找代码完成(如果你觉得通过这样做可以更好地帮助其他人 – 继续),但是或多或少的指导或建议正确的方向来完成一个嵌套数组,如上面的直接.

解决方法:

我假设所有标题都在DOM中处于同一级别,因此每个h3都是h2的兄弟.有了这个假设,你可以迭代h2的兄弟,直到遇到下一个h2:

foreach($dom->getElementsByTagName('h2') as $node) {

$key = $dom->saveHtml($node);

$matches[$key] = array();

while(($node = $node->nextSibling) && $node->nodeName !== 'h2') {

if($node->nodeName == 'h3') {

$matches[$key][] = $dom->saveHtml($node);

}

}

}

标签:php,dom,parsing,html-parsing,domdocument

来源: https://codeday.me/bug/20190728/1566286.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值