Select Page
This entry has been published on 2014-05-23 and may be out of date.

Last Updated on 2014-05-23.

Scenario

After upgrading from PHP 5.3 to 5.5, Smarty returns empty strings when using e.g. “html_options” etc., if the “values” or “output” array contains special characters like German Umlaute (ä, ü, …).

Reason

From PHP 5.4 upwards, the function “htmlspecialchars” uses charset UTF-8 by default. This change causes the function to return an empty string if the .php file (or the parameter string) is not UTF-8 encoded.

Unfortunately, Smarty uses this function in many cases. If the mbstring extension is enabled in PHP, Smarty supposes you use UTF-8 by default, and also passes this parameter to htmlspecialchars.

For htmlspecialchars, it also makes no difference if you set another default charset in php.ini.

Workaround

If you do not want to rewrite and debug all your PHP scripts, add the charset definition for Smarty explicitly, e.g.:

define('SMARTY_RESOURCE_CHAR_SET', 'ISO-8859-1');

You have to insert the line before you include the Smarty.class.php file.

Source: http://www.smarty.net/docs/en/charset.tpl

In other scripts (independent of Smarty), you should change the htmlspecialchars parameters, e.g.:

htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES, 'ISO-8859-1');

Note

Dealing with different character sets and regarding function changes coming with updates is always horrible.

If you have the chance, for new projects set everything possible to UTF-8 from the beginning:

  • Database, like MySQL
  • HTML header (content type etc.)
  • The PHP code files themselves. Most editors let you choose the charset.