Jump to content

How to echo output with DOM


vinpkl

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/258535-how-to-echo-output-with-dom/
Share on other sites

$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.

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.

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.

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);

 

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.