Theresa Arzadon-Labajo

Replace TeX with HTML characters

Posted by Theresa Arzadon-Labajo (tarzadon) on Jul 06 2009
Tech Stuff >>

We use FPDF to grab data from a database and output to PDF.  Some fields in the database contain TeX markup.

Here is the function I came up with in order to search for TeX markup and replace with HTML characters.  I only put in the characters that we come across often, as the array could get real large.

    function ReplaceTeX($tex)
    {
        $search = array ("/\\\\\'e/",    // Matches \'e
                 "/\\'e/",               // 'e
                 "/\\\\\`E/",
                 "/\\\\\'E/",
                 "/\\\\\`e/",
                 "/\\\\\'e/",
                 '/\\\\"a/',
                 "/\\\\\`a/",
                 "/\\\\\'a/",
                 "/\\\\\^a/",
                 "/\\\\\`i/",
                 "/\\\\\'i/",
                 "/\\\\ae/",
                 "/\\\\AE/",
                 "/\\\\oe/",
                 "/\\\\o/",
                 "/\\\\O/",
                 "/\\\\\^O/",
                 '/\\\\"u/',
                 '/\\\\"o/');

        $replace = array (chr(233), //eacute
                 chr(233), //eacute without slash
                 chr(200), //E grave
                 chr(201), //E acute
                 chr(233), //e grave
                 chr(234), //e accute
                 chr(228), //a diaeresis
                 chr(224), //agrave
                 chr(225), //aacute
                 chr(226), //a circumflex
                 chr(236), //igrave
                 chr(237), //iacute
                 chr(230), //aelig
                 chr(198), //Capital aelig
                 chr(339), //oe
                 chr(248), //oslash
                 chr(216), //capital oslash
                 chr(212), //capital o circumflex
                 chr(252), //uuml
                 chr(246)); //ouml

        $texhtml = preg_replace($search, $replace, $tex);
        return $texhtml;
    }

Then to call the function in FPDF:

    $sometext=$pdf->ReplaceTeX($sometext);

Last changed: Jul 06 2009 at 5:55 PM

Back

Comments

useful By Guest on Nov 29 2011 at 6:17 PM
thanks! very useful

I used your suggestion for php and pdf generation.

Btw, it is missing ouml capital and uuml capital

Ouml Uuml (capital) By Guest on Nov 29 2011 at 6:20 PM
chr(220) =>'U', //capital uuml
chr(214) =>'O' //capital ouml
Thanks By Theresa Arzadon-Labajo on Dec 20 2011 at 5:24 PM
Thanks for pointing out the missing characters.
I will add them to the array.

Add Comment