3

I have this HTML Template:

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

I would like to create a page for editing the text of this Template and the Tags attribute.

For doing this, i need to parse this html into a php array and load the page.

This is an hypothetical array that I could get from the html written above:

$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'
            )

        )

    );

At the moment I have tried to use this Class: https://code.google.com/p/php-html2array/downloads/detail?name=class.htmlParser.php The problem is that the class is returning only the tag, while the text without tags should be ignored like "Out Text 1 Edit" OR "Out Text 2 Edit"

So the given array is

(
[-{}-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
                            (
                            )

                    )

            )

    )

)

Any idea to parse the html into an array? (I have searched how the browsers parse the html code and show it in the console, like chrome or firebug, and they permit the edit)

I know that parse html with a regex is hard or impossible, is there another solution?

Thank you all in advance, sorry for my poor english

Best regards Andrea.

Andrea Catania
  • 1,361
  • 3
  • 21
  • 37
  • 4
    We need [bobince](http://stackoverflow.com/a/1732454/2454376)... – mishik Aug 05 '13 at 14:11
  • Have you tried [`http://php.net/simplexml`](simplexml)? It won't give you what you desire, but it is a starting point – Carlos Campderrós Aug 05 '13 at 14:13
  • 1
    Take a look at PHP's [DOMDocument](http://php.net/manual/en/class.domdocument.php) –  Aug 05 '13 at 14:14
  • i hadn't used "simplexml" but i want see if it's possible for do that. i need a function like this in jquery: http://api.jquery.com/jQuery.parseHTML/ – Andrea Catania Aug 05 '13 at 14:33
  • @mishik my coworkers are wondering why I'm laughing so hard right now... But yeah you can't parse html with with regex – Chris Frank Aug 05 '13 at 14:42
  • @ChrisFrank i don't think I had asked how can i parse html with a regex, but if there is another solution... – Andrea Catania Aug 05 '13 at 14:49
  • @AndreaCatania http://stackoverflow.com/a/10366491/1547278 – Chris Frank Aug 05 '13 at 14:53
  • @ChrisFrank it's good idea but my template is dynamic and i don't know the specific tag name. if there is a function for navigate between tag without the name, would be perfect – Andrea Catania Aug 05 '13 at 15:08
  • @AndreaCatania If you have a set of tags which will be dynamically created you could use 'or' in the foreach call. Or you could use http://simplehtmldom.sourceforge.net/manual.htm – Chris Frank Aug 05 '13 at 15:21

2 Answers2

0

If you familiar with jQuery, you can use phpQuery - it's basically the php port. Easy, pretty fast, and well documented.

Yura Sokolov
  • 207
  • 4
  • 13
  • @ChrisFrank I tryed with a jquery, this is the code 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() ); }); but it return only the

    and
    tag without free text like "Out Text 1 Edit" or "Out Text 1 Edit"

    – Andrea Catania Aug 06 '13 at 07:25
  • Because it's wrong code. If you want to get tag name and it's content, you can use something like this $('div#test').each(fucntion(){ alert($(this).attr('tagName') + ' - ' + $(this).html()); }); – Yura Sokolov Aug 06 '13 at 09:27
0

Thanks for your advices I've made the function you can see below.

It won't give to me what I desire, but it is a good starting point. When I will have the final solution i will post it for you guys Thanks agine for your help.

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 . '>' );

        }

     }

}
Andrea Catania
  • 1,361
  • 3
  • 21
  • 37