vinpkl Posted March 8, 2012 Share Posted March 8, 2012 Hi all i m using DOM code to strip the inline styles. The problem is that when i "view source" my code in browser then i see 2 <html>tags and 2<doctype> tags starting <?php require_once("config.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> here is the second doctyle starts <? $html = '<table> <tbody> <tr style="PADDING-BOTTOM: 0px; BORDER-RIGHT-WIDTH: 0px;"> <td style="BACKGROUND-IMAGE: none; BORDER-BOTTOM: rgb(240,240,240) 1px solid;">2G Network</td> <td style="BACKGROUND-COLOR: rgb(250,250,250); MARGIN: 0px;">GSM 850 / 900 / 1800 / 1900</td> </tr> </tbody> </table>'; $doc = new DOMDocument(); $doc->loadHTML($html); $search = new DOMXPath($doc); $results = $search->evaluate('//*[@style]'); foreach ($results as $result) $result->removeAttribute('style'); $newhtml = $doc->saveHTML(); echo $newhtml; ?> </body> </html> This is the view source from browser <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> here is the second doctyle starts <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><table><tbody><tr> <td>2G Network</td> <td>GSM 850 / 900 / 1800 / 1900</td> </tr></tbody></table></body></html> </body> </html> so can anyone tell me how to use and echo result from DOM but with validated html Vineet Quote Link to comment https://forums.phpfreaks.com/topic/258535-how-to-echo-output-with-dom/ Share on other sites More sharing options...
batwimp Posted March 8, 2012 Share Posted March 8, 2012 Does your included config.php echo display the extra ones? Quote Link to comment https://forums.phpfreaks.com/topic/258535-how-to-echo-output-with-dom/#findComment-1325263 Share on other sites More sharing options...
vinpkl Posted March 8, 2012 Author Share Posted March 8, 2012 Does your included config.php echo display the extra ones? config.php doesnt echo anything. its just for connecting to database vineet Quote Link to comment https://forums.phpfreaks.com/topic/258535-how-to-echo-output-with-dom/#findComment-1325266 Share on other sites More sharing options...
batwimp Posted March 8, 2012 Share Posted March 8, 2012 $doc = new DOMDocument(); $doc->loadHTML($html); $search = new DOMXPath($doc); $results = $search->evaluate('//*[@style]'); foreach ($results as $result) $result->removeAttribute('style'); $newhtml = $doc->saveHTML(); echo $newhtml; The new DOMDocument creates a DOCTYPE and <html> tags. So you are doubling up because you are also echoing out your own. Quote Link to comment https://forums.phpfreaks.com/topic/258535-how-to-echo-output-with-dom/#findComment-1325276 Share on other sites More sharing options...
vinpkl Posted March 8, 2012 Author Share Posted March 8, 2012 hi batwimp Is there any solution by which i can use this same DOM code and html tags are loaded once vineet $doc = new DOMDocument(); $doc->loadHTML($html); $search = new DOMXPath($doc); $results = $search->evaluate('//*[@style]'); foreach ($results as $result) $result->removeAttribute('style'); $newhtml = $doc->saveHTML(); echo $newhtml; The new DOMDocument creates a DOCTYPE and <html> tags. So you are doubling up because you are also echoing out your own. Quote Link to comment https://forums.phpfreaks.com/topic/258535-how-to-echo-output-with-dom/#findComment-1325279 Share on other sites More sharing options...
DavidAM Posted March 8, 2012 Share Posted March 8, 2012 strip it out $newhtml = preg_replace('~</body></html>$~i', '', preg_replace('~^<!DOCTYPE.*<body>~i', '', $doc->saveHTML())); I'm no RegExpert, and I'm pretty sure some of those characters need to be escaped, but unless DOCDocument provides a way to retrieve it without the tags, you will just have to strip them out. Quote Link to comment https://forums.phpfreaks.com/topic/258535-how-to-echo-output-with-dom/#findComment-1325391 Share on other sites More sharing options...
kicken Posted March 8, 2012 Share Posted March 8, 2012 try echo $doc->saveHTML($doc->documentElement)); Quote Link to comment https://forums.phpfreaks.com/topic/258535-how-to-echo-output-with-dom/#findComment-1325392 Share on other sites More sharing options...
vinpkl Posted March 9, 2012 Author Share Posted March 9, 2012 hi kicken i get this error by using your code Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in F:\xampp\htdocs\style_strip1.php on line 30 this is my full code $doc = new DOMDocument(); $doc->loadHTML($html); $search = new DOMXPath($doc); $results = $search->evaluate('//*[@style]'); foreach ($results as $result) $result->removeAttribute('style'); $newhtml = $doc->saveHTML(); /*echo $newhtml;*/ echo $doc->saveHTML($doc->documentElement); Quote Link to comment https://forums.phpfreaks.com/topic/258535-how-to-echo-output-with-dom/#findComment-1325468 Share on other sites More sharing options...
silkfire Posted March 9, 2012 Share Posted March 9, 2012 What PHP version you got? Quote Link to comment https://forums.phpfreaks.com/topic/258535-how-to-echo-output-with-dom/#findComment-1325470 Share on other sites More sharing options...
vinpkl Posted March 9, 2012 Author Share Posted March 9, 2012 i got php version 5 and working in xampp What PHP version you got? Quote Link to comment https://forums.phpfreaks.com/topic/258535-how-to-echo-output-with-dom/#findComment-1325473 Share on other sites More sharing options...
Muddy_Funster Posted March 9, 2012 Share Posted March 9, 2012 The way I look at it, DOMDocumet does what it does because it is meant to do it that way. So rather than hacking at the results it gives how about having a look through this: http://www.ultramegatech.com/2009/07/generating-xhtml-documents-using-domdocument-in-php/ and making it do what your really trying to achieve? Quote Link to comment https://forums.phpfreaks.com/topic/258535-how-to-echo-output-with-dom/#findComment-1325476 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.