tave/forceutf8

There is no license information available for the latest version (v1.0.0) of this package.

PHP Class Encoding featuring popular Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strings.

v1.0.0 2014-09-12 16:31 UTC

This package is not auto-updated.

Last update: 2019-03-08 21:41:20 UTC


README

PHP Class Encoding featuring popular \ForceUTF8\Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strings.

Description

If you apply the PHP function utf8_encode() to an already-UTF8 string it will return a garbled UTF8 string.

This class addresses this issue and provides a handy static function called Encoding::toUTF8().

You dont need to know what the encoding of your strings is. It can be Latin1 (iso 8859-1), Windows-1252 or UTF8, or the string can have a mix of them. Encoding::toUTF8() will convert everything to UTF8.

Sometimes you have to deal with services that are unreliable in terms of encoding, possibly mixing UTF8 and Latin1 in the same string.

Update:

I've included another function, Encoding::fixUTF8(), wich will fix the double (or multiple) encoded UTF8 string that looks garbled.

Usage:

$utf8_string = \ForceUTF8\Encoding::toUTF8($utf8_or_latin1_or_mixed_string);

$latin1_string = Encoding::toLatin1($utf8_or_latin1_or_mixed_string);

also:

$utf8_string = \ForceUTF8\Encoding::fixUTF8($garbled_utf8_string);

Examples:

echo \ForceUTF8\Encoding::fixUTF8("Fédération Camerounaise de Football");
echo \ForceUTF8\Encoding::fixUTF8("FÃédÃération Camerounaise de Football");
echo \ForceUTF8\Encoding::fixUTF8("FÃÃédÃÃération Camerounaise de Football");
echo \ForceUTF8\Encoding::fixUTF8("FÃÃÃédÃÃÃération Camerounaise de Football");

will output:

Fédération Camerounaise de Football
Fédération Camerounaise de Football
Fédération Camerounaise de Football
Fédération Camerounaise de Football

Composer Install:

I don't want to submit this to Packagist.org unless I get requests to do so.

You'll need to add a repository and require statement to your composer.json like so:

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/jpirkey/forceutf8"
        }
    ],

    "require": {
        "jpirkey/forceutf8" : "master"
    },
}