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.

9
  • 4
    We need bobince... Commented Aug 5, 2013 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 Commented Aug 5, 2013 at 14:13
  • 1
    Take a look at PHP's DOMDocument Commented Aug 5, 2013 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: api.jquery.com/jQuery.parseHTML Commented Aug 5, 2013 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 Commented Aug 5, 2013 at 14:42

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

2 Comments

@ChrisFrank I tryed with a jquery, this is the code var str = '<div id="test"><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>'; $(str).children().each( function( index, value ){ alert( index+' - '+$(value).contents() ); }); but it return only the <p> and <br> tag without free text like "Out Text 1 Edit" or "Out Text 1 Edit"
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()); });
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 . '>' );

        }

     }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.