Charset Converter

Charsets are a recurring problem in PHP.  Typically there are three possible spaces where charsets can be different in an Amfphp service: The text in your database, the text in your PHP objects and the text in the data that you actually return to your client. Conversion from one to the other has to be carefully managed as your text can end up mangled.

The best solution to avoid any trouble is to use UTF-8 everywhere. In that case, no conversion is necessary.

If you cannot for some reason use UTF-8 everywhere, then the AmfphpCharsetConverter plugin can help you, or can at least be used as a source of inspiration. The plugin can help you convert your text from your PHP object encoding to your client encoding, typically UTF-8. However it does not support having three different charsets, one for the DB, one for PHP and one for the client. If you need that option, you will need to handle conversion from DB to PHP yourself.

The most common case is that your DB is not using UTF-8 but some other encoding like ISO-8859-1, a common encoding in western Europe. Your client expects UTF-8. Let’s take that as an example.

The plugin takes 3 parameters: clientCharset, phpCharset, and method.

In our example, we must:

– change phpCharset to ISO-8859-1.

– change method to something other than ‘none’, which is the default. Which method to use depends on what is available on your server and also what conversion you want to make. Iconv is a good choice if it’s installed on your server. So let’s suppose that the server has the iconv lib installed, and use it. That means therefore setting method to ‘iconv’. See all the possible codepages for iconv here: http://www.gnu.org/software/libiconv/

– clientCharset defaults to « utf-8 », so we can leave it alone.

That means that the plugin must receive the following object as a config:

$charsetConfig = array('phpCharset' => 'ISO-8859-1', 'method' => 'iconv');

If you do this in your Amfphp_Core_Config class constructor:


$this->pluginsConfig['AmfphpCharsetConverter'] = $charsetConfig;

See Configuring Amfphp section for more information on how to configure your plugins.

See here for the generated doc.

Laissez un commentaire

News letter

Evénements Silex Labs sur Paris et sa région: Inscrivez vous à la Newsletter mensuelle

Silex Labs community Tweets

Facebook page