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
Other answers
It is commonly done using the libxml PHP extension using appropriate options for HTML.
Toby Thain
Related Q & A:
- What is the best way to distribute an audio/video feed from a computer to TVs over existing indoor coax cable?Best solution by Audio-Video Production
- What is the best way to clean LEGO bricks?Best solution by bricks.stackexchange.com
- What is the easiest way to play any sound file?Best solution by Stack Overflow
- What is the best way to count lines in file?Best solution by Stack Overflow
- What is the best way to learn PHP?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.