Tag Archives: special characters

FPDF: How to use a Degree Symbol in a Generated PDF Tips, Tricks and Tutorials 29 SEP 2017

I’ve been using the FPDF PDF generator library for years now as the de facto method for my PHP projects to produce PDF reports. However, one minor annoyance is that the generated PDF files often falter when it comes to the inclusion of certain special characters – like the degree symbol (°) as an example.  (Basically, something like °C becomes °C in the final document)

The reason for this happening is that Arial, the default used/included font, is of type ISO-8859-1 while the degree symbol is UTF-8 encoded. So in order for us to include special symbols or characters from other languages, we need to either try and convert them into our font compatible ISO-8859-1 format, or perhaps switch to using a different TrueType or Type1 font (which then would contain the desired character set).

Now while UTF-8 support is available via a modified class, the easiest way to fix the degree symbol issue without having doing any real work is to simply make use of the PHP utf8_decode function, which convert UTF-8 encoded strings to their ISO-8859-1 equivalents.

In other words outputting utf8_decode(“°C”) to your PDF should result in the expected °C

Related Link: FPDF PDF Generator Library

PHP: Handle Microsoft Windows Smart Quotes in Your Strings CodeUnit 04 JUL 2011

Microsoft Word likes converting single and double quotes into it’s so-called smart quotes representation, which can be annoying when you are trying to display the string on a device that doesn’t support smart quotes (the curly ones in case you were wondering). The easiest way to clean out these curly smart or fancy quotes is to run a simple search and replace on your string:

 $text = "string containing Microsoft Smart Quotes...";
 $chrs = array (chr(150), chr(147), chr(148), chr(146));
 $repl = array ("-", """, """, "'");
 $text = str_replace($chrs, $repl, $text);

And now you know.

PHP: How to Gracefully deal with some Microsoft Windows Special Characters CodeUnit 22 JUN 2011

Although Microsoft Windows smart quotes are the usual culprits for web developers who need to display strings on devices powered by operating systems other than Windows, there are in fact a number of other Windows-only special characters that can potentially rear their heads every now and then.

Below is a handy list of string replaces (done verbosely for easier reading) that will replace these Windows special characters with accepted alternatives which can more easily be ported across to different systems:

 $str = str_replace(chr(130), ',', $str);    // baseline single quote
 $str = str_replace(chr(131), 'NLG', $str);  // florin
 $str = str_replace(chr(132), '"', $str);    // baseline double quote
 $str = str_replace(chr(133), '...', $str);  // ellipsis
 $str = str_replace(chr(134), '**', $str);   // dagger (a second footnote)
 $str = str_replace(chr(135), '***', $str);  // double dagger (a third footnote)
 $str = str_replace(chr(136), '^', $str);    // circumflex accent
 $str = str_replace(chr(137), 'o/oo', $str); // permile
 $str = str_replace(chr(138), 'Sh', $str);   // S Hacek
 $str = str_replace(chr(139), '<', $str);    // left single guillemet
 $str = str_replace(chr(140), 'OE', $str);   // OE ligature
 $str = str_replace(chr(145), "'", $str);    // left single quote
 $str = str_replace(chr(146), "'", $str);    // right single quote
 $str = str_replace(chr(147), '"', $str);    // left double quote
 $str = str_replace(chr(148), '"', $str);    // right double quote
 $str = str_replace(chr(149), '-', $str);    // bullet
 $str = str_replace(chr(150), '-', $str);    // endash
 $str = str_replace(chr(151), '--', $str);   // emdash
 $str = str_replace(chr(152), '~', $str);    // tilde accent
 $str = str_replace(chr(153), '(TM)', $str); // trademark ligature
 $str = str_replace(chr(154), 'sh', $str);   // s Hacek
 $str = str_replace(chr(155), '>', $str);    // right single guillemet
 $str = str_replace(chr(156), 'oe', $str);   // oe ligature
 $str = str_replace(chr(159), 'Y', $str);    // Y Dieresis

Should be useful.