将HTML解析为PHP数组

3

我有这个 HTML 模板:

<div>
  <p class="ex-fr">Tex1 - Edit</p>

  Out Text 1 Edit

  <p>Tex2 - Edit</p>

  Out Text 1 Edit

  <br>

  Out Text 3 Edit

</div>

我希望创建一个页面来编辑此模板的文本和标签属性。

为了实现这个目标,我需要将这个 HTML 解析成 PHP 数组并加载页面。

下面是一个假设的数组,可能是从上面的 HTML 中获取的:

$parsedHtml = array(
        'thisIs'=>'tag',
        'tag' => 'div',
        'attr' => '',
        'children'=> array(
            0 => array(
                'thisIs'=>'tag',
                'tag' => 'p',
                'attr' => 'class="ex-fr"',
                'children'=> array(
                    'thisIs'=>'text',
                    'tag' => '',
                    'attr' => '',
                    'children'=> 'Tex1 - Edit'
                )
            ),
            1 => array(
                'thisIs'=>'text',
                'tag' => '',
                'attr' => '',
                'children'=> 'Out Text 1 Edit'
            ),
            2 => array(
                'thisIs'=>'tag',
                'tag' => 'p',
                'attr' => '',
                'children'=> array(
                    'thisIs'=>'text',
                    'tag' => '',
                    'attr' => '',
                    'children'=> 'Tex2 - Edit'
                )
            ),
            3 => array(
                'thisIs'=>'text',
                'tag' => '',
                'attr' => '',
                'children'=> 'Out Text 2 Edit'
            ),
            4 => array(
                'thisIs'=>'sTag',
                'tag' => 'br',
                'attr' => '',
                'children'=> ''
            ),
            5 => array(
                'thisIs'=>'text',
                'tag' => '',
                'attr' => '',
                'children'=> 'Out Text 3 Edit'
            )

        )

    );

目前我尝试使用这个类: https://code.google.com/p/php-html2array/downloads/detail?name=class.htmlParser.php 问题在于该类仅返回标签,而不应包含文本内容(如“Out Text 1 Edit”或“Out Text 2 Edit”)。
因此,给定的数组是:
(
[-{}-2-0-{}-] => Array
    (
        [id] => -{}-2-0-{}-
        [father] => 
        [tag] => div
        [innerHTML] =>  <p class='ex-fr'>Tex1 - Edit</p> Out Text 1 Edit <p>Tex2 - Edit</p> Out Text 1 Edit <br> Out Text 3 Edit 
        [htmlText] => <div > <p class='ex-fr'>Tex1 - Edit</p> Out Text 1 Edit <p>Tex2 - Edit</p> Out Text 1 Edit <br> Out Text 3 Edit </div>
        [stratr] => 
        [childNodes] => Array
            (
                [0] => Array
                    (
                        [id] => -{}-1-0-{}-
                        [father] => -{}-2-0-{}-
                        [tag] => p
                        [innerHTML] => Tex1 - Edit
                        [htmlText] => <p class='ex-fr'>Tex1 - Edit</p>
                        [stratr] =>  class='ex-fr'
                        [childNodes] => Array
                            (
                            )

                    )

                [1] => Array
                    (
                        [id] => -{}-1-1-{}-
                        [father] => -{}-2-0-{}-
                        [tag] => p
                        [innerHTML] => Tex2 - Edit
                        [htmlText] => <p>Tex2 - Edit</p>
                        [stratr] => 
                        [childNodes] => Array
                            (
                            )

                    )

                [2] => Array
                    (
                        [id] => -{}-0-0-{}-
                        [father] => -{}-2-0-{}-
                        [tag] => br
                        [innerHTML] => <br>
                        [htmlText] => <br>
                        [stratr] => 
                        [childNodes] => Array
                            (
                            )

                    )

            )

    )

)

有没有将HTML解析为数组的方法? (我已经搜索了浏览器如何解析HTML代码并在控制台中显示它,比如Chrome或Firebug,并且它们允许编辑)
我知道使用正则表达式解析HTML很难或者不可能,还有其他的解决方法吗?
提前感谢大家,对我的糟糕英语表示歉意。
最好的问候,Andrea。

4
我们需要 bobince…… - mishik
你尝试过http://php.net/simplexml吗?它可能无法给你想要的结果,但它是一个起点。 - Carlos Campderrós
1
请查看PHP的DOMDocument - user1864610
我之前没有使用过"simplexml",但我想看看是否有可能做到。 我需要一个像这样的jQuery函数:http://api.jquery.com/jQuery.parseHTML/ - Andrea Catania
@ChrisFrank 我不认为我曾经问过如何使用正则表达式解析HTML,但如果有其他解决方案... - Andrea Catania
显示剩余4条评论
2个回答

0

感谢您的建议,我已经编写了下面的函数。

虽然它不能给我想要的结果,但这是一个很好的起点。 当我有最终解决方案时,我会为大家发布。再次感谢您的帮助。

function parseHtml( $parent ){

    foreach( pq( $parent )->contents() as $children ){
        echo '<br>';
        $a = isset( $children->tagName );
        if( $a ){
            echo htmlentities( '<' . $children->tagName . '>' );

        }else{
            echo '<br>';
            echo '"' . htmlentities( $children->textContent ) . '"';
            echo '<br>';
        }


        parseHtml( $children );

        if( $a ){
            echo htmlentities( '</' . $children->tagName . '>' );

        }

     }

}

0

如果您熟悉 jQuery,那么您可以使用 phpQuery - 它基本上是 PHP 的端口。易于使用、速度较快,并且文档齐全。


@ChrisFrank 我尝试使用 jQuery,这是代码: var str = '

Tex1 - Edit

Out Text 1 Edit

Tex2 - Edit

Out Text 1 Edit
Out Text 3 Edit
'; $(str).children().each( function( index, value ){ alert( index+' - '+$(value).contents() ); });
但它只返回 <p> 和 <br> 标签,而没有像 "Out Text 1 Edit" 或 "Out Text 1 Edit" 这样的自由文本。
- Andrea Catania
因为它是错误的代码。如果你想获取标签名和其内容,你可以使用类似以下的代码:$('div#test').each(fucntion(){ alert($(this).attr('tagName') + ' - ' + $(this).html()); }); - Yura Sokolov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接