Tidy PHP: Fatal Error: Class ‘tidy’ Not Found
I was playing around with Tidy on my development machine at work, however even simple examples copied directly from the PHP manual were giving me errors such as:
1 | Fatal error: Class 'tidy' not found in /var/www/dev.test.domain.org/tidy.php on line 149 |
I checked my PHP version, which is 5.2.6, and made sure I had the php5-tidy Ubuntu package installed. All was well on those fronts, yet I still had the problem.
I thought maybe the extension wasn’t loading, but running the following confirmed that it was indeed loaded:
1 | <?php echo extension_loaded('tidy') ? "LOADED" : "NOT LOADED" ?> |
I tested a bit further using the procedural syntax. More oddness ensued. I used the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php ob_start() ?> <html> <head> <title>test</title> </head> <body> <p>error<br>another ĨńtêrʼnåtȉΌnժlizǽtioǸ line</i> </body> </html> <?php $buffer = ob_get_clean(); $tidy_config = array( 'clean' => true, 'output-xhtml' => true, 'wrap' => 200, ); $tidy = tidy_parse_string($buffer, $tidy_config, 'UTF8'); $tidy->cleanRepair(); echo $tidy; ?> |
Which resulted in the following error:
1 | Warning: tidy_parse_string() expects exactly 1 parameter, 3 given in /var/www/dev.test.domain.org/tidy.php on line 147 |
This made me suspicious. The fact that the tidy_parse_string function existed but did not expect the documented number of parameters made me suspect I had somehow got the wrong version installed. Sure enough, on further inspection I found I had at some point in the past installed the PECL version of tidy, which is 1.2. This was obviously taking precedence over the version installed via the php5-tidy package.
So the fix was pretty simple:
Remove the PECL version of Tidy:
1 | # pecl uninstall tidy |
For good measure I uninstalled the php5-tidy package too, but this is probably unnecessary:
1 | # apt-get remove php5-tidy |
Restart Apache (again probably not necessary at this point but I did it anyway):
1 | # apache2ctl restart |
Re-installed the php5-tidy package:
1 | # apt-get install php5-tidy |
Restart Apache:
1 | # apache2ctl restart |
After this everything worked as expected.