How parse html in php?
I know we can use PHP DOM to parse HTML using PHP. I found a lot of questions here on Stack Overflow too. But I have a specific requirement. I have an HTML content like below
I want to parse the above HTML and save the content into two different arrays like:
I can achieve this simply using jQuery. But I am not sure, if that's the right way. It would be great if someone can point me to the right direction. Thanks in advance.
hatef 4,60328 gold badges44 silver badges43 bronze badges asked Aug 21, 2013 at 4:55
6 I have used domdocument and domxpath to get the solution, you can find it at:
Live result: http://saji89.codepad.org/2TyOAibZ answered Aug 21, 2013 at 5:45
saji89saji89 1,9934 gold badges25 silver badges49 bronze badges 1 Try to look at PHP Simple HTML DOM Parser It has brilliant syntax similar to jQuery so you can easily select any element you want by ID or class
answered Aug 21, 2013 at 4:58
4
Here's an alternative way to parse the html using
answered Dec 25, 2020 at 6:11
8ctopus8ctopus 1,9712 gold badges14 silver badges20 bronze badges 1 // Create DOM from URL or file
// Find all images
// Find all links
Chen-Tsu Lin 22.2k16 gold badges51 silver badges63 bronze badges answered Mar 5, 2014 at 7:55
jfraberjfraber 5891 gold badge5 silver badges6 bronze badges 2 Can PHP read HTML file?If you want to run your HTML files as PHP, you can tell the server to run your . html files as PHP files, but it's a much better idea to put your mixed PHP and HTML code into a file with the . php extension.
How do you parse HTML?HTML parsing involves tokenization and tree construction. HTML tokens include start and end tags, as well as attribute names and values. If the document is well-formed, parsing it is straightforward and faster. The parser parses tokenized input into the document, building up the document tree.
Can we parse HTML?HTML is a markup language with a simple structure. It would be quite easy to build a parser for HTML with a parser generator. Actually, you may not need even to do that, if you choose a popular parser generator, like ANTLR. That is because there are already available grammars ready to be used.
What is parsing in PHP?The parse_str() function is a built-in function in PHP which parses a query string into variables. The string passed to this function for parsing is in the format of a query string passed via a URL. Syntax : parse_str($string, $array)
|