tapaskaro Posted March 17, 2015 Share Posted March 17, 2015 Hi, I am very new to PHP and trying to read elements of an HTML page. I was able to do below global $phpname; $episodePage = file_get_contents($htmlpage); $dom = new DOMDocument(); $dom->loadHTML($episodePage); However, from the $dom, I want to read a particular element (with ID = XXXXXXXXXXX). I want to get the entire HTML code inside that element. I know that there is function called $dom ->getElementsByTagName(" "). I also tried $dom->getElementById("XXXXXXXX") but this wont' give me anything. Any idea, how to get the actual HTML source for that entire HTML element. Any help would really appreciate. Quote Link to comment Share on other sites More sharing options...
requinix Posted March 17, 2015 Share Posted March 17, 2015 getElementById() is what your next step will be, but as its name says it gets you an element - not an HTML string. Get the element, then pass it to $dom's saveHTML to get the HTML string. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 10, 2015 Share Posted April 10, 2015 Also be aware, for this to work, the HTML content needs to be 100% valid. The majority of webpages on the internet are NOT valid HTML. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted April 10, 2015 Share Posted April 10, 2015 Also be aware, for this to work, the HTML content needs to be 100% valid. The majority of webpages on the internet are NOT valid HTML. I'm not sure if that's true... The following comes from the online documentation for loadHTML(): Unlike loading XML, HTML does not have to be well-formed to load. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 10, 2015 Share Posted April 10, 2015 Unless you trust the HTML to be reasonably valid, it'd probably a good idea to run it through something like Tidy first. Just in case. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.