| 1 |
forceutf8 |
| 2 |
========= |
| 3 |
|
| 4 |
PHP Class Encoding featuring popular \ForceUTF8\Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strings. |
| 5 |
|
| 6 |
Description |
| 7 |
=========== |
| 8 |
|
| 9 |
If you apply the PHP function utf8_encode() to an already-UTF8 string it will return a garbled UTF8 string. |
| 10 |
|
| 11 |
This class addresses this issue and provides a handy static function called \ForceUTF8\Encoding::toUTF8(). |
| 12 |
|
| 13 |
You don't 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. \ForceUTF8\Encoding::toUTF8() will convert everything to UTF8. |
| 14 |
|
| 15 |
Sometimes you have to deal with services that are unreliable in terms of encoding, possibly mixing UTF8 and Latin1 in the same string. |
| 16 |
|
| 17 |
Update: |
| 18 |
|
| 19 |
I've included another function, \ForceUTF8\Encoding::fixUTF8(), which will fix the double (or multiple) encoded UTF8 string that looks garbled. |
| 20 |
|
| 21 |
Usage: |
| 22 |
====== |
| 23 |
|
| 24 |
use \ForceUTF8\Encoding; |
| 25 |
|
| 26 |
$utf8_string = Encoding::toUTF8($utf8_or_latin1_or_mixed_string); |
| 27 |
|
| 28 |
$latin1_string = Encoding::toLatin1($utf8_or_latin1_or_mixed_string); |
| 29 |
|
| 30 |
also: |
| 31 |
|
| 32 |
$utf8_string = Encoding::fixUTF8($garbled_utf8_string); |
| 33 |
|
| 34 |
Examples: |
| 35 |
|
| 36 |
use \ForceUTF8\Encoding; |
| 37 |
|
| 38 |
echo Encoding::fixUTF8("Fédération Camerounaise de Football\n"); |
| 39 |
echo Encoding::fixUTF8("Fédération Camerounaise de Football\n"); |
| 40 |
echo Encoding::fixUTF8("Fédération Camerounaise de Football\n"); |
| 41 |
echo Encoding::fixUTF8("Fédération Camerounaise de Football\n"); |
| 42 |
|
| 43 |
will output: |
| 44 |
|
| 45 |
Fédération Camerounaise de Football |
| 46 |
Fédération Camerounaise de Football |
| 47 |
Fédération Camerounaise de Football |
| 48 |
Fédération Camerounaise de Football |
| 49 |
|
| 50 |
Options: |
| 51 |
======== |
| 52 |
By default, `Encoding::fixUTF8` will use the `Encoding::WITHOUT_ICONV` flag, signalling that iconv should not be used to fix garbled UTF8 strings. |
| 53 |
|
| 54 |
This class also provides options for iconv processing, such as `Encoding::ICONV_TRANSLIT` and `Encoding::ICONV_IGNORE` to enable these flags when the iconv class is utilized. The functionality of such flags are documented in the [](http://php.net/manual/en/function.iconv.phpPHP iconv documentation](http://php.net/manual/en/function.iconv.php](http://php.net/manual/en/function.iconv.php). |
| 55 |
|
| 56 |
Examples: |
| 57 |
|
| 58 |
use \ForceUTF8\Encoding; |
| 59 |
|
| 60 |
$str = "Fédération Camerounaise—de—Football\n"; // Uses U+2014 which is invalid ISO8859-1 but exists in Win1252 |
| 61 |
echo Encoding::fixUTF8($str); // Will break U+2014 |
| 62 |
echo Encoding::fixUTF8($str, Encoding::ICONV_IGNORE); // Will preserve U+2014 |
| 63 |
echo Encoding::fixUTF8($str, Encoding::ICONV_TRANSLIT); // Will preserve U+2014 |
| 64 |
|
| 65 |
will output: |
| 66 |
|
| 67 |
Fédération Camerounaise?de?Football |
| 68 |
Fédération Camerounaise—de—Football |
| 69 |
Fédération Camerounaise—de—Football |
| 70 |
|
| 71 |
while: |
| 72 |
|
| 73 |
use \ForceUTF8\Encoding; |
| 74 |
|
| 75 |
$str = "čęėįšųūž"; // Uses several characters not present in ISO8859-1 / Win1252 |
| 76 |
echo Encoding::fixUTF8($str); // Will break invalid characters |
| 77 |
echo Encoding::fixUTF8($str, Encoding::ICONV_IGNORE); // Will remove invalid characters, keep those present in Win1252 |
| 78 |
echo Encoding::fixUTF8($str, Encoding::ICONV_TRANSLIT); // Will trasliterate invalid characters, keep those present in Win1252 |
| 79 |
|
| 80 |
will output: |
| 81 |
|
| 82 |
???????? |
| 83 |
šž |
| 84 |
ceeišuuž |
| 85 |
|
| 86 |
|
| 87 |
Install via composer: |
| 88 |
===================== |
| 89 |
Edit your composer.json file to include the following: |
| 90 |
|
| 91 |
```json |
| 92 |
{ |
| 93 |
"require": { |
| 94 |
"neitanod/forceutf8": "~2.0" |
| 95 |
} |
| 96 |
} |
| 97 |
``` |
| 98 |
|
| 99 |
Tips: |
| 100 |
===== |
| 101 |
You can tip me with Bitcoin if you want. :) |
| 102 |
|
| 103 |
<img src="resources/wallet.jpg" width="225" alt="1Awfu4TZpy99H7Pyzt1mooxU1aP2mJVdHP"> |
| 104 |
|