6

I was writing some code for repair html string. I read some nice solutions which work with the Tidy PHP class but I had some troubles with it. What in this post is written, is exactly what I want but I need to install / load the PHP Tidy class. Close tags from a truncated HTML string

I'm working on PHP 5.5.4. I tried to install tidy following some tutorials but nothing has append. When I call the tidy class $tidi = new \tidy();, NetBeans suggests me the class and clicking on it (Ctrl + click) I see it but refreshing the page I obtain the error Class 'tidy' not found in ... /myfile.php line ...

I used in the same way the class $myVar = new \DomDocument(); but it works perfectly.

Checking whether the Tidy extension is loaded like below, I get "NOT LOADED".

echo extension_loaded('tidy') ? "LOADED" : "NOT LOADED";

Please, can someone explain me how Tidy works and how can I set it up? My Ubuntu's version is 13.10.

Community
  • 1
  • 1
Roberto Rizzi
  • 1,525
  • 5
  • 26
  • 39
  • Did you enable the extension in your php.ini file? Does your extensions folder contain the necessary extension? What kind of PHP environment are you running (Xampp, loacl apache, LAMP server...)? – ToBe Jan 29 '14 at 15:30
  • I tried to enable it but i didn't understand very well how i can do it. Can you explain me what i have to enable and how can i include the extension? I installed LAMP server separately. thanks for the quick response – Roberto Rizzi Jan 29 '14 at 16:07
  • I ran into this because the [PHP docs](http://php.net/manual/en/tidy.installation.php) misleadingly say "This extension is bundled with PHP 5 and greater, and is installed using the --with-tidy configure option." and I believed them. I had to do a yum install as the answers below allude to. – Patrick M Mar 21 '18 at 20:17

2 Answers2

10

Your php.ini contains a list of "extension=somefile.so" lines. You are missing the correct line for your tidy extension. It should be included but commented in your file. Just remove the ; character in front of the one line for the tidy extension and restart your webserver.

Sample for windows:

extension=php_tidy.dll

Sample for Linux:

extension=tidy.so

On some Linux distribution, it has to be installed if you can't find the extension file tidy.so in your extensions folder. Execute the correct line depending on your distribution. You have to be root or use sudo:

CentOS/RedHat (replace with correct lib as found via yum search php-tidy:

yum install rh-php56-php-tidy.x86_64

Debian/Ubuntu:

apt-get install php5-tidy
ToBe
  • 2,667
  • 1
  • 18
  • 30
  • Thank you very much but nothing is working yet. Then, i tried simply adding `extension=tidy.so` to the _/etc/php5/apache2/php.ini_ and, restarting apache nothing is happened. After that i prompted a _locate tidy.so_ and it gave _/usr/lib/libtidy.so_. Then i set `extension_dir = "/usr/lib"` but nothing is still happened. If i launch `sudo apt-get install php5-tidy` i have an error – Roberto Rizzi Jan 29 '14 at 17:10
  • Don't change the extensions folder. What distribution are you using? Did you get any startup errors when just enabling the so? – ToBe Jan 29 '14 at 17:12
  • 1
    It should be : yum install rh-php56-php-tidy.x86_64 – Dylan B Feb 21 '17 at 16:52
  • @DylanB Updated. – ToBe May 29 '17 at 19:43
4

I had the same issue and I finnally figured it out, this solution is for ubuntu 14.04 running xampp.

step 1
open terminal and input the following

sudo apt-get install php5-tidy

step 2
time to find out where "tidy.so" got installed to.


for me it was installed to "/usr/lib/php5/20121212/tidy.so"

step 3

find and open your php.ini file and find the extensions section and insert the following line

extension=/usr/lib/php5/20121212/tidy.so

step 4

go back to your terminal and run the following to restart your server.

sudo /opt/lampp/lampp restart

step 5

verifying that the new tidy module has been loaded by either

in terminal

php -m

and/or by creating a file on your server in the htdocs called test.php and inserting the following code

<?php echo extension_loaded('tidy') ? "LOADED" : "NOT LOADED"; ?>

next run that test.php file in your browser and it should read "LOADED"

if not go back and review the previous steps until you get that message.

I hope this helps, :)