tags:

views:

606

answers:

3

How can I keep HTML Tidy from converting PHP short tags when used as values in html attributes?

Here's an example of what it currently does. It converts this:

<input value='<?=$variable?>'>

to this:

<input value='&lt;?=$variable?&gt;'>

I want HTML Tidy to ignore PHP short tags. Any config options that change this?

==

To simplify, is there a way to have HTML Tidy just avoid doing HTML entity conversion? If it would just not convert < and >, that would solve my problem.

A: 

Even if it could be done Tidy doesn't work too well with php - it might choke if your php code contains quote marks etc. Also Tidy might throw warnings about missing attributes if they're output by php.

You could replace all <?$variable?> to <#$variable#> before running Tidy and then replace them back. In the console, for example like this:

sed -i 's/<?=\(.*\)?>/<#\1#>/g' yourfile.php
tidy -m yourfile.php
sed -i 's/<#\(.*\)#>/<?=\1?>/g' yourfile.php

The first line converts <?=$variable?> to <#$variable#> inside file, the second runs tidy in place and updates the file, The third line converts <#$variable#> back to <?=$variable>.

enkrs
+1  A: 

Unprocessed PHP is not compatible with HTML (except few most trivial cases), and Tidy has only superficial support for it.

There are countless ways in which Tidy can actually cause errors in the document, because it won't understand what PHP generates and how it interacts with other markup.

To get correct and reliable results you should postprocess HTML-only output. You can do that by adding Tidy filter in PHP:

<?php
ob_start('ob_tidyhandler');
?>

This will impact runtime performance, but it's not a problem for most sites as Tidy is quite fast.

porneL
A: 

You could pre-process your php:

by adding comments by converting <? to <!--<?, and ?> to ?>-->

<input value='<?=$variable?>'>

would become

<input value='<!--<?=$variable?>-->'>

after running HTMLtidy, you would do the opposite.

  1. pre-process by adding comment tags
  2. run HTMLtidy
  3. un-pre-process...
Osama ALASSIRY