How to properly parse HTML and XML in PHP? Understanding DOMDocument, SimpleXML and regular expressions
Hello everyone! I'm a beginner PHP developer and I've been struggling with parsing HTML pages for the second week now. I need to extract all links and headings from an external site, but I can't figure out how to do it correctly.\n\nAt first, I tried using regular expressions, but the code turned out terrible and breaks at the slightest change in HTML. Here's what I had:\n\n```php\n$html = file_get_contents('https://example.com');\npreg_match_all('/(.*?)<\\/a>/', $html, $matches);\n```\n\nThis only works for simple links, but if there are quotes or line breaks in the attributes — everything breaks. And I'm not even talking about XML — there are namespaces and complex nesting.\n\nThen I came across `DOMDocument` and `SimpleXML`, but I don't understand the difference and which one to choose. I've also heard about `DOMXPath` and `DOMCrawler`, but
No answers yet.
Log in to leave an answer