|
Posted by Christoph Burschka on 04/26/07 20:18
webrod wrote:
> Hi,
>
> do you know a tool like TIDY which allows to validate the HTML code
> with PHP tags?
>
> TIDY is not good enough because it removes a lot of php code!!
>
> Thanks for your help..
>
> Rod
>
Hello Rod,
Maybe a funny question, but why is there PHP code on your page at the time it is
filtered? Isn't the purpose of a filter to process the _output_ - after the PHP
code has already been parsed and executed?
I can think of a few cases where you might want to filter static PHP files
(inherited old site that needs to be brought to XHTML compliance, perhaps), but
those aren't so common that someone would build PHP "pass-through" support into
an XHTML filter.
After all, if your PHP code is going to produce any output itself, this wouldn't
be checked by the filter, so it may break the page even after the validator
fixed it.
About the only things I can suggest is that you either refactor your site so
that the PHP code is first executed and then filtered for XHTML compliance, or
(if that's really not possible), use something like this to remove PHP code and
then put it back in into the parsed code (untested, possibly buggy).
--------------------------------------
global $tags;
$tags = array();
$text = "whatever you want to filter";
$text = preg_replace('/<\?(php|=| ).*\?>/es','save_tag(\'$0\')',$text);
// PHP tags are now stored in order of occurrence in the $tags array, and
replaced by comment tags.
// Now run $text through the Tidy filter. Then...
$text = preg_replace('/<!-- PHP TAG ([0-9]+) -->/','restore_tag(\'$1\')',$text);
// PHP tags are now back in the filtered code. Here are the functions called above:
function save_tag($tag)
{
global $tags;
$tags[]=$tag; // add tag to the global variable
return "<!-- PHP TAG ".count($tags)." -->"; // replace with placeholder
}
function restore_tag($tag_id)
{
global $tags;
return $tags[$tag_id]; // revert placeholder to php tag
}
-------------------------------------
Note that the regular expressions are very primitive - for example, it will
break when it encounters something like <?php print "?>" ?>. It's really only a
messy workaround.
--
cb
Navigation:
[Reply to this message]
|