TheStudent2023 Posted May 2, 2023 Share Posted May 2, 2023 (edited) Php Devs, What is wrong with this code ? I see blank page. Where did I go wrong on these two ? Note where the $message variable is on both. I get undefined $message if I leave it at the bottom. So, I tried rearranging to the 2 codes you see below. 1. // Initiate ability to manipulate the DOM and load that baby up $doc = new DOMDocument(); libxml_use_internal_errors(true); // Because we are actually manipulating the DOM, DOMDocument will add complete <html><body> tags we need to strip out //$message = str_replace(array('<body>', '</body>'), '', $doc->saveHTML($doc->getElementsByTagName('body')->item(0))); $message = file_get_contents('https://www.rocktherankings.com/post-sitemap.xml'); $doc->loadHTML($message, LIBXML_NOENT|LIBXML_COMPACT); libxml_clear_errors(); // Fetch all <a> tags $links = $doc->getElementsByTagName('a'); // If <a> tags exist ... if ($links->length > 0) { // For each <a> tag ... foreach ($links AS $link) { $link->setAttribute('class', 'link-style'); } } 2. // Initiate ability to manipulate the DOM and load that baby up $doc = new DOMDocument(); // Because we are actually manipulating the DOM, DOMDocument will add complete <html><body> tags we need to strip out //$message = str_replace(array('<body>', '</body>'), '', $doc->saveHTML($doc->getElementsByTagName('body')->item(0))); $message = file_get_contents('https://www.rocktherankings.com/post-sitemap.xml'); //$message = str_replace(array('<body>', '</body>'), '', $doc->saveHTML($doc->getElementsByTagName('body')->item(0))); libxml_use_internal_errors(true); $doc->loadHTML($message, LIBXML_NOENT|LIBXML_COMPACT); libxml_clear_errors(); // Fetch all <a> tags $links = $doc->getElementsByTagName('a'); // If <a> tags exist ... if ($links->length > 0) { // For each <a> tag ... foreach ($links AS $link) { $link->setAttribute('class', 'link-style'); } } Edited May 2, 2023 by TheStudent2023 Link to comment https://forums.phpfreaks.com/topic/316242-why-i-see-blank-page/ Share on other sites More sharing options...
ginerjm Posted May 2, 2023 Share Posted May 2, 2023 you don't have error checking enabled. That might help you. Link to comment https://forums.phpfreaks.com/topic/316242-why-i-see-blank-page/#findComment-1607938 Share on other sites More sharing options...
Solution TheStudent2023 Posted May 4, 2023 Author Solution Share Posted May 4, 2023 (edited) @ginerjm Lol! It is working now: ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(E_ALL); //Sitemap Protocol: https://www.sitemaps.org/protocol.html // Initiate ability to manipulate the DOM and load that baby up $doc = new DOMDocument(); // Because we are actually manipulating the DOM, DOMDocument will add complete <html><body> tags we need to strip out //$message = str_replace(array('<body>', '</body>'), '', $doc->saveHTML($doc->getElementsByTagName('body')->item(0))); $message = file_get_contents('https://www.daniweb.com/programming/web-development/threads/538868/simplehtmldom-failing#post2288453'); libxml_use_internal_errors(true); $doc->loadHTML($message, LIBXML_NOENT|LIBXML_COMPACT); libxml_clear_errors(); // Fetch all <a> tags $links = $doc->getElementsByTagName('a'); // If <a> tags exist ... if ($links->length > 0) { // For each <a> tag ... foreach ($links AS $link) { $link->setAttribute('class', 'link-style'); } } $message = str_replace(array('<body>', '</body>'), '', $doc->saveHTML($doc->getElementsByTagName('body')->item(0))); Edited May 4, 2023 by TheStudent2023 Link to comment https://forums.phpfreaks.com/topic/316242-why-i-see-blank-page/#findComment-1608039 Share on other sites More sharing options...
TheStudent2023 Posted May 4, 2023 Author Share Posted May 4, 2023 @ginerjm But you keep saying RTFM. And so, I am. But stuck to find something. So care to help me find it ? I can't find this particular line on the DomDocument Parser docs: $doc->loadHTML($message, LIBXML_NOENT|LIBXML_COMPACT); libxml_clear_errors(); The rest of the code, I found in the doc lastnight.https://www.php.net/domdocument And incase you are wondering how I manage to cook up the code, then answer is: I did not. Got it from somewhere. Link to comment https://forums.phpfreaks.com/topic/316242-why-i-see-blank-page/#findComment-1608040 Share on other sites More sharing options...
Barand Posted May 4, 2023 Share Posted May 4, 2023 https://www.php.net/manual/en/domdocument.loadhtml.php Link to comment https://forums.phpfreaks.com/topic/316242-why-i-see-blank-page/#findComment-1608042 Share on other sites More sharing options...
TheStudent2023 Posted May 4, 2023 Author Share Posted May 4, 2023 (edited) 1 hour ago, Barand said: https://www.php.net/manual/en/domdocument.loadhtml.php No. I am talking about this part, which me failing to find in the doc: .....IBXML_NOENT|LIBXML_COMPACT); libxml_clear_errors(); The error part. Edited May 4, 2023 by TheStudent2023 Link to comment https://forums.phpfreaks.com/topic/316242-why-i-see-blank-page/#findComment-1608047 Share on other sites More sharing options...
TheStudent2023 Posted May 6, 2023 Author Share Posted May 6, 2023 @requnix Do close this thread as issue solved. Ypu may add an input, if you want. Link to comment https://forums.phpfreaks.com/topic/316242-why-i-see-blank-page/#findComment-1608129 Share on other sites More sharing options...
Recommended Posts