PHP preg_match_all 重复出现模式

发布于 2024-11-06 12:44:29 字数 1112 浏览 3 评论 0原文

我正在尝试获取开放标签的匹配项。存在打开父标签(包括子标签)的问题。父标签被捕获,但忽略子标签。

前任。

</p>
<p>hello world</p>
<p><img

preg_match_all('/<(\/?[a-z]+)[^>]*\/?>/i', $trimmed_text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);

将给出以下输出:

Array
 (
[0] => Array
    (
        [0] => Array
            (
                [0] => 


                [1] => 0
            )

        [1] => Array
            (
                [0] => /p
                [1] => 1
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [0] => 

[1] => 5 ) [1] => Array ( [0] => p [1] => 6 ) ) [2] => Array ( [0] => Array ( [0] =>

                [1] => 19
            )

        [1] => Array
            (
                [0] => /p
                [1] => 20
            )

    )

[3] => Array
    (
        [0] => Array
            (
                [0] => 

 [1] => 24 ) [1] => Array ( [0] => p [1] => 25 ) ) ) 

父级中任何打开的标签是否有可能拥有子集数组?

I am trying to grab a match of open tags. Having an issue where a parent tag is opened including a child tag. The parent tag is captured but it ignores the children tags.

ex.

</p>
<p>hello world</p>
<p><img

preg_match_all('/<(\/?[a-z]+)[^>]*\/?>/i', $trimmed_text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);

would give the following output:

Array
 (
[0] => Array
    (
        [0] => Array
            (
                [0] => 


                [1] => 0
            )

        [1] => Array
            (
                [0] => /p
                [1] => 1
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [0] => 

[1] => 5 ) [1] => Array ( [0] => p [1] => 6 ) ) [2] => Array ( [0] => Array ( [0] =>

                [1] => 19
            )

        [1] => Array
            (
                [0] => /p
                [1] => 20
            )

    )

[3] => Array
    (
        [0] => Array
            (
                [0] => 

 [1] => 24 ) [1] => Array ( [0] => p [1] => 25 ) ) ) 

Is it possible for any opened tags in a parent to have a subset array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人心善变 2024-11-13 12:44:29

你做得很困难,使用 PHP Simple HTML DOM Parser 来解析html,

例如:

// Create DOM from URL or file
include('simple_html_dom.php');
$html = file_get_html('http://www.scroogle.org/');

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

// Find all links
foreach($html->find('a') as $element)
       echo $element->href . '<br>'; 

you're doing it the hard way, use PHP Simple HTML DOM Parser to parse html,

ex:

// Create DOM from URL or file
include('simple_html_dom.php');
$html = file_get_html('http://www.scroogle.org/');

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

// Find all links
foreach($html->find('a') as $element)
       echo $element->href . '<br>'; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文