Reputation: 11931
I have an irritating problem using PHP's DOMdocument. I have loaded HTML, and changed some of the element's attributes. I want to save the changed HTML, and output it.
The strange thing is, when I use ->saveHTML()
or ->saveXML()
my closing tags' slashes become escaped. I could remove the escaping with regex, but I would like to know if there is any cleaner way...
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML ($roosterHTML);
$dom->preserveWhiteSpace = false;
libxml_clear_errors();
libxml_use_internal_errors(false);
$tables = $dom->getElementsByTagName('table');
$cols = $tables->item(0)->getElementsByTagName('td');
$name = preg_replace("/(\\n|\\r| )/", "", $cols->item(3)->nodeValue);
$sirname = preg_replace("/(\\n|\\r| )/", "", $cols->item(2)->nodeValue);
$class = preg_replace("/(\\n|\\r| )/", "", $cols->item(1)->nodeValue);
$header = "Rooster van $name $sirname ($class)";
$rooster = $tables->item(1);
$firstRow = true;
foreach ($rooster->getElementsByTagName('tr') as $row) {
if ($firstRow) {
$firstRow = false;
continue;
}
$firstCol = true;
foreach ($row->getElementsByTagName('td') as $col) {
if ($firstCol) {
$firstCol = false;
continue;
}
$text = $col->nodeValue;
$col->setAttribute('style','background-color:#FF0');
//$return.= $text;
}
}
$rooster = $dom->saveXML($rooster);
Testing (just click submit, to send a POST value): http://bit.ly/ymK3DA
Upvotes: 0
Views: 198
Reputation: 47321
No, the escaped is caused by the json
which mean this page is not output HTML but json-alike plain text
Upvotes: 1