2017 © Pedro Peláez
 

library forceutf8

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

image

neitanod/forceutf8

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

  • Thursday, September 21, 2017
  • by neitanod
  • Repository
  • 89 Watchers
  • 1124 Stars
  • 1,757,461 Installations
  • PHP
  • 46 Dependents
  • 0 Suggesters
  • 277 Forks
  • 12 Open issues
  • 9 Versions
  • 9 % Grown

The README.md

forceutf8

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

Description

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

This class addresses this issue and provides a handy static function called \ForceUTF8\Encoding::toUTF8()., (*3)

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., (*4)

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

Update:, (*6)

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

Usage:

use \ForceUTF8\Encoding;

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

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

also:, (*8)

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

Examples:, (*9)

use \ForceUTF8\Encoding;

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

will output:, (*10)

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

Options:

By default, Encoding::fixUTF8 will use the Encoding::WITHOUT_ICONV flag, signalling that iconv should not be used to fix garbled UTF8 strings., (*11)

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., (*12)

Examples:, (*13)

use \ForceUTF8\Encoding;

$str = "Fédération Camerounaise—de—Football\n"; // Uses U+2014 which is invalid ISO8859-1 but exists in Win1252
echo Encoding::fixUTF8($str); // Will break U+2014
echo Encoding::fixUTF8($str, Encoding::ICONV_IGNORE); // Will preserve U+2014
echo Encoding::fixUTF8($str, Encoding::ICONV_TRANSLIT); // Will preserve U+2014

will output:, (*14)

Fédération Camerounaise?de?Football
Fédération Camerounaise—de—Football
Fédération Camerounaise—de—Football

while:, (*15)

use \ForceUTF8\Encoding;

$str = "čęėįšųūž"; // Uses several characters not present in ISO8859-1 / Win1252
echo Encoding::fixUTF8($str); // Will break invalid characters
echo Encoding::fixUTF8($str, Encoding::ICONV_IGNORE); // Will remove invalid characters, keep those present in Win1252
echo Encoding::fixUTF8($str, Encoding::ICONV_TRANSLIT); // Will trasliterate invalid characters, keep those present in Win1252

will output:, (*16)

????????
šž
ceeišuuž

Install via composer:

Edit your composer.json file to include the following:, (*17)

{
    "require": {
        "neitanod/forceutf8": "~2.0"
    }
}

Tips:

You can tip me with Bitcoin if you want. :), (*18)

1Awfu4TZpy99H7Pyzt1mooxU1aP2mJVdHP, (*19)

The Versions

21/09 2017

dev-master

9999999-dev https://github.com/neitanod/forceutf8

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

  Sources   Download

The Requires

  • php >=5.3.0

 

by Sebastián Grignoli

22/05 2017

v2.x-dev

2.9999999.9999999.9999999-dev https://github.com/neitanod/forceutf8

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

  Sources   Download

The Requires

  • php >=5.3.0

 

by Sebastián Grignoli

22/05 2017

v2.0.1

2.0.1.0 https://github.com/neitanod/forceutf8

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

  Sources   Download

The Requires

  • php >=5.3.0

 

by Sebastián Grignoli

13/09 2016

dev-patch-1

dev-patch-1 https://github.com/jpirkey/forceutf8

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

  Sources   Download

The Requires

  • php >=5.2.0

 

07/05 2015

2.x-dev

2.9999999.9999999.9999999-dev https://github.com/neitanod/forceutf8

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

  Sources   Download

The Requires

  • php >=5.3.0

 

by Sebastián Grignoli

07/05 2015

v2.0

2.0.0.0 https://github.com/neitanod/forceutf8

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

  Sources   Download

The Requires

  • php >=5.3.0

 

by Sebastián Grignoli

02/01 2015

1.x-dev

1.9999999.9999999.9999999-dev https://github.com/neitanod/forceutf8

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

  Sources   Download

The Requires

  • php >=5.3.0

 

by Sebastián Grignoli

02/01 2015

v1.5

1.5.0.0 https://github.com/neitanod/forceutf8

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

  Sources   Download

The Requires

  • php >=5.3.0

 

by Sebastián Grignoli

24/09 2014

v1.4

1.4.0.0 https://github.com/neitanod/forceutf8

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

  Sources   Download

The Requires

  • php >=5.2.0

 

by Sebastián Grignoli