首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在每个h2标记之后获取内容

在每个h2标记之后获取内容
EN

Stack Overflow用户
提问于 2020-09-10 07:45:14
回答 2查看 656关注 0票数 0

我试图使用php在每个h2标记之后(以及在下一个h2标记之前)提取内容。

示例:

代码语言:javascript
复制
$content = '<h2>title 1</h2>
<ul>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>
<h2>title 2</h2>
<p>testing only</p>
<p>testing only</p>
<p>testing only</p>
<h2>title 3</h2>
<p>testing only</p>
<p>testing only</p>';

成为

代码语言:javascript
复制
[0] => <ul>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>

[1] => <p>testing only</p>
<p>testing only</p>
<p>testing only</p>

[2] => <p>testing only</p>
<p>testing only</p>

我尝试了很多不同的东西,太多了,无法在这里列出。我只想提取h2标记之间的内容,而不是h2标记本身。

如果有人能指出我的正确方向,或帮助我,那将是非常感谢的!

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-10 08:16:15

试试看这个:)

代码语言:javascript
复制
<?php

    $content = "your content";

    preg_match_all('/(?:<\/h2>)(.*?)(?:<h2>|\z)/s', $content, $match);

    var_dump($match);
?>

演示-> https://www.phpliveregex.com/p/x7j (选择,preg_match_all)

编辑

注意,如果您问自己为什么会有一个多维数组作为匹配结果:

  • $matches[0]是一个全模式匹配

的数组。

  • $matches[1]是由第一个带括号的子模式匹配的字符串数组.

  • $matches[2]是第二个带括号的子模式匹配的字符串数组.

  • (.),等等

如果要检查preg_match_all是否成功,请注意在继续之前检查$match[0]。如果你想检查你的匹配组,请注意检查例如。$1 -> $match[1],$2 -> $match[2],$3 -> $match[3] (.)等等;

如果多次匹配,则匹配组将包含多个结果。

示例:单一匹配

https://phpsandbox.io/n/icy-term-9wp6

代码语言:javascript
复制
<?php
    $test_string = "Your task XX-123";

    preg_match_all('/task (([A-Z]{1,2})-([0-9]{1,}))/s', $test_string, $match);

    // destruct your array is equal to selection by index $match[$index]
    [$full_match, $match_group_1, $match_group_2, $match_group_3] = $match;

    var_dump($full_match);    // -> ["task XX-123"]
    var_dump($match_group_1); // -> ["XX-123"]
    var_dump($match_group_2); // -> ["XX"]
    var_dump($match_group_3); // -> ["123"]
?>

示例:多匹配

https://phpsandbox.io/n/shy-credit-0ng6

代码语言:javascript
复制
<?php
    $test_string = "Your task XX-123, Your task YZ-456, Your task CD-789";

    preg_match_all('/task (([A-Z]{1,2})-([0-9]{1,}))/s', $test_string, $match);

    // destruct your array is equal to selection by index $match[$index]
    [$full_match, $match_group_1, $match_group_2, $match_group_3] = $match;

    var_dump($full_match);    // -> ["task XX-123", "task YZ-456", "task CD-789"]
    var_dump($match_group_1); // -> ["XX-123", "YZ-456", "CD-789"]
    var_dump($match_group_2); // -> ["XX", "YZ", "CD"]
    var_dump($match_group_3); // -> ["123", "456", "789"]
?>

示例:句柄错误

https://phpsandbox.io/n/bitter-morning-55gn

代码语言:javascript
复制
<?php

    $test_string = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy";

    preg_match_all('/(no)-(matching)-(pattern)/s', $test_string, $match);

    // get your defined match groups
    $full_match = $match[0];
    $match_group_1 = $match[1];
    $match_group_2 = $match[2];
    $match_group_3 = $match[3];

    // check if your match was successfull
    if (empty($full_match)) {
        // handle error
        print("could not match any result");
        var_dump($match);
    }
    // handle success
    else {
        print("matched something, check $match values for more details");
        var_dump($match_group_1, $match_group_2, $match_group_3);
    }

?>

见php.net docs -> https://www.php.net/manual/en/function.preg-match-all.php

票数 0
EN

Stack Overflow用户

发布于 2020-09-10 19:58:57

这是我的建议:

代码语言:javascript
复制
$content = '<h2>title 1</h2>
<ul>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>
<h2>title 2</h2>
<p>testing only</p>
<p>testing only</p>
<p>testing only</p>
<h2>title 3</h2>
<p>testing only</p>
<p>testing only</p>';

$content = preg_replace('/<h2>(.*?)<\/h2>/s', '|', $content);
$content = explode('|', $content);
$content = array_map('trim', array_values(array_filter($content)));

// var_dump($content);

按照OP的请求,它只返回一个数组。

我相信这是可以改进的。但我觉得这是个很好的起点。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63825082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档