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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.