Is there a clean way to parse HTML?

What is the best way to parse an HTML file in PHP?

  • I have html file, it has many <p> tags ie:-       <p>content 1</p>       <p>content 2</p>       <p>content 3</p> how can i read all <p> tag elements  data and store in an array for example my output should be like this :        array(             [0]=>content 1             [1]=>content 2             [2]=>content 3                    )

  • Answer:

    Although you can do it by hand by writing your own parser, take a look at this library that S.C. Chen graciously shares: http://simplehtmldom.sourceforge.net/ Your code will look something like this: // Create DOM from URL or file $html_text = "<p>some html</p>"; $html = file_get_html(str_get_html($html_text)); // Find all paragraphs (P elements) foreach($html->find('p') as $element) echo $element->innertext . '<br>';

Joseph Hsieh at Quora Visit the source

Was this solution helpful to you?

Other answers

It is commonly done using the libxml PHP extension using appropriate options for HTML.

Toby Thain

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.