PHP preg_match_all reoccuring模式

时间:2011-05-11 19:17:14

标签: php regex preg-match preg-match-all

我正试图抓住一个开放标签的匹配。遇到包含子标签的父标签打开的问题。捕获父标记但忽略子标记。

离。

</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 ) ) ) 

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

1 个答案:

答案 0 :(得分:1)

你正在努力做到这一点,使用 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>';