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 を解析することは困難または不可能であることを知っています。別の解決策はありますか?

よろしくお願いします。下手な英語でごめんなさい

よろしくアンドレア。

4

2 に答える 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 . '>' );

        }

     }

}
于 2013-08-06T10:54:49.810 に答える