Page 1 of 1

PHP5 DOM/Tidy

Posted: Sun Apr 24, 2005 1:24 pm
by p3x
Hello,

I was playing with the new features of PHP5 (the Tidy support and the DOM support).

I wrote the following code:

Code: Select all

<?php
$dom = new domdocument;
$dom->load("test2.html");
$dom->formatOutput = TRUE;

$params = $dom->getElementsByTagName('table');

foreach ($params as $param) {
	if ($param -> getAttribute('border')) {
      	$param -> setAttribute('border', 0);
      }
}
$dom->saveHTMLFile('test.html');

$options = array("output-xhtml" => true, "clean" => true, "indent" => true, "indent-spaces" => 4, "wrap" => 100);
$tidy = tidy_parse_file("test.html", $options);
tidy_clean_repair($tidy);

$fp = fopen('test.html', 'w');
fwrite($fp, $tidy);
fclose($fp);
?>
It opens a file (test2.html), checks all the table tags, and sets the border value to 0. So far so good, the problem arises with saving the DOM generated output to a file. The DOM functions only supports output to XML or HTML, not XHTML. In order to have valid XHTML markup, I had to use the Tidy functions to 'fix' the test.html file I just saved... then save that file again. Seems like a lot of work...

Does anyone have a better solution? or ideas?

p3x

Posted: Mon Apr 25, 2005 10:07 am
by timvw
use the functions that make use of a string instead of a file:

http://www.php.net/manual/en/function.d ... vehtml.php
http://www.php.net/tidy_parse_string

Posted: Mon Apr 25, 2005 10:29 am
by p3x
Thanks,
Was just about to post that I found the solution, and that was exactly the solution I found, using the string functions :)

p3x