UTF-8 Encoding

For most PHP sites UTF-8 is the default character set and using this class is not needed. This class is useful for forcing different character sets to UTF-8 prior to JSON or XML encoding.

The PHP function [json_encode()] requires UTF-8 prior to encoding, otherwise it will trigger an error:
    JSON_ERROR_UTF8 - 'Malformed UTF-8 characters, possibly incorrectly encoded'

For example, IBM i-Series Servers use EBCDIC character encoding which correctly translates to UTF-8 for most characters, however it will trigger the error for some characters.

Another example is on FastSitePHP's Encryption Unit Tests, an array of binary strings need to be converted to UTF-8 prior to JSON encoding and hashing. Using this for binary data in most situations is not recommended, rather if you are working with binary files or data use binary format directly.

Source Code




Static Function

Encode data to UTF-8

Returns: string