Auto repair XHTML code with PHP

Tidy PHP extension allows to automate fix XHTML and XML code. I wrote Symfony Plugin that provides automated code repair (ex. users input from TinyMCE or FCKEditor). The sfTidyHelper produce valid XHTML or XML code. If you done use Symfony you will find in this entry function repairing broken code.

To automaticaly cleanup and repair xhtml code (ex. from textarea or input tag) use Tidy extesnion for PHP.
Sample broken code which can be fixed

1
<p>ala <b> ma kota & psa<br />Lorem ipsum

to valid XHTML:

1
<p>ala <b> ma kota &amp; psa<br />Lorem ipsum</b></p>

Simple function which repair wrong code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * Use php tidy extension to cleanup code.
 *
 * @param string $text
 * @return string
 * @author Rafal Zelazko <rafal [at] zelazko [dot] info>
 */

function tidy($text)
{
  if (function_exists('tidy_parse_string'))
  {
    $config = array('indent' => true,
      'output-xhtml' => true,
      'wrap' => 200);
    $tidy = tidy_parse_string($text, $config, 'UTF8');
    $body = tidy_get_body($tidy);
    $text = preg_replace('/< \/?body[^>]*>/im', '', $body->value);
    return trim($text);
  }

  trigger_error('tidy extension not found', E_USER_WARNING);
  return $text;
}

I wrote Symfony Framework Plugin – http://www.symfony-project.org/plugins/sfTidyPlugin with TidyHelper which implements it.

Author: Rafał Żelazko

Freelancer. Experienced J2EE, PHP, JS Developer. Advanced Linux user. WebPerfekt.pl owner.

Leave a Reply

Your email address will not be published. Required fields are marked *