PHP5 DOM/Tidy

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
p3x
Forum Newbie
Posts: 9
Joined: Thu Nov 11, 2004 4:35 am

PHP5 DOM/Tidy

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
p3x
Forum Newbie
Posts: 9
Joined: Thu Nov 11, 2004 4:35 am

Post 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
Post Reply