Jun
13

Auto repair XHTML code with PHP

By rzelazko  //  PHP  //  No Comments

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.

Leave a comment