create dom html with php, getelementbyid doesn’t work

I leverage the domdocument php class to create elegant, dynamic html documents via the dom tree. However, I was finding that the “getelementbyid” method was not returning elements as expected. The reason for this is that I had not defined a DTD, which is necessary in this case because we only specify that we are saving as HTML at the end of the process, so the domdocument object is only understood as generic XML, which has no predefined “id.” To create a DTD use this code (here using 4.01 transitional html):

$doc = new DOMDocument();
$doc->loadHTML(‘<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>;’);

Leave Comment