Development #html#xml#programming 3 min

tidy: Converting HTML to XHTML

Translation of the original in Catalan. Read it in: CatalàCastellano

You have probably already read that I have had, and probably still have, problems formatting HTML content into XHTML, which is the format the new versions of BLOG:CMS work with and in theory the format of the future of web pages. Since through style sheets you get an almost 100% independence of the content and of the content's format. If you want more info on the subject I recommend you drop by Diego's website ( mini-d ) there you will find much more information about these formats and their advantages.

So in this article I will try to explain how, using PHP, I have tried to automatically convert the HTML code of my previous articles and comments to the new XHTML 1.0 transitional format. As with all migration processes there is some manual retouching, but the important thing is that the script saves us as much work as possible.

Well then, as the title says I have used tidy to do this, this software is an extension for PHP that lets us automate the conversion. In fact they are a collection of very useful functions, you can look at the documentation for them on the php website.

In fact, most of the tidy functions are for PHP5 and I obviously don't use that version of PHP, but version PHP 4.3.8, so I sweated for a good while until I discovered, by trial and error, which functions worked in PHP4, which by the way, are not specified in the fantastic manual from the php people.

To install tidy, with the mini-manual that is on php.net I had enough, although what it does not say is that you have to edit the php.ini file and add the following line so that the tidy extension is loaded:

extension = tidy.so

You can put more options in the php.ini file but honestly I have ignored them. To know that it works correctly, I recommend you run phpinfo(); and look for the tidy default options box, if it is not there it means you still don't have the support loaded. Remember that after touching the php.ini you have to restart apache.

The PHP4 code I finally used to do the conversion is the following:

$opts = array(	"clean" => true,	"output-xhtml" => true,	"drop-font-tags" => true,	"drop-empty-paras" => true,	"wrap" => 80,	"logical-emphasis" => true,	"enclose-text" => true,	"enclose-block-text" => true );	$html="Codi en format HTML<br>Per provar <b>que tot funcioni</b>";$xhtml=tidy_repair_string($html,$opts);

As for the options I use, I recommend you take them from Clean up your Web pages with HTML TIDY this document has a section where the options of the converter are described quite well. As for the rest of the code it will return the XHTML code with all the headers and footers and the usual XML formalism baggage. If, like me, what you want is to send this code to the DDBB because they are article, comment and similar contents, what you will want is to remove those headers, so that you don't have to think about how to do it here is my code:

$inici = strpos ($xhtml,"<body>")+6;$final = strpos ($xhtml,"</body>");$final = $final - $inici;$article = substr ( $sortida, $inici, $final);

Now inside the article variable you will have something like this:

<p>Codi en format HTML<br />Per provar <strong>que tot funcioni</strong>

Even though it seems very simple I don't want to tell you the hours it has taken me to discover this or you would think I'm retarded . In fact, I have my brain so wrecked after trying to correct all this that I feel like sending it all to hell. On the other hand, I'm sure there are still things that don't quite work on the portal, I hope you help me find them so I can correct them. Thanks.

3 comments

Comments from the old blog, as they were written. Comments are closed.

  1. JUAN

    HOLA

  2. obokaman

    EL tidy, el vas instal.lar sota windows o sota linux? Estic provant d'instalar-lo a una red hat, no només per convertir articles antics, si no per passar una validació previa a l'enmagatzemament en base de dades, i me les estic veient canutes...

    Merci noi.

  3. —

    [2] sota linux, és clar. Vaig usar el portage de gentoo: app-text/tify concretament tinc instal·lada la versió 4.8.6 ara mateix. No sé si puc fer algo més per ajudar-te.