| @@ -9,9 +9,9 @@ | ||
| 9 | 9 | If you apply the PHP function utf8_encode() to an already-UTF8 string it will return a garbled UTF8 string. |
| 10 | 10 | |
| 11 | 11 | This class addresses this issue and provides a handy static function called \ForceUTF8\Encoding::toUTF8(). |
| 12 | 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. | |
| 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 | 14 | |
| 15 | 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 | 16 | |
| 17 | 17 | Update: |
| @@ -45,9 +45,46 @@ | ||
| 45 | 45 | Fédération Camerounaise de Football |
| 46 | 46 | Fédération Camerounaise de Football |
| 47 | 47 | Fédération Camerounaise de Football |
| 48 | 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. | |
| 49 | 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 [PHP iconv documentation](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 | + | |
| 50 | 87 | Install via composer: |
| 51 | 88 | ===================== |
| 52 | 89 | Edit your composer.json file to include the following: |
| 53 | 90 | |
| @@ -58,4 +95,9 @@ | ||
| 58 | 95 | } |
| 59 | 96 | } |
| 60 | 97 | ``` |
| 61 | 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"> | |