Created
July 11, 2012 14:09
-
-
Save michael-cannon/3090584 to your computer and use it in GitHub Desktop.
Prevent doubly encoding UTF-8 content when incoming data could be UTF-8, ASCII or another character set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$recTitle = $record['title']; | |
$encoding = mb_detect_encoding( $recTitle, 'UTF-8', true ); | |
if ( empty( $encoding ) ) { | |
$recTitle = mb_convert_encoding( $recTitle, 'UTF-8' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The third parameter of mb_detect_encoding is our trick. Strict specifies whether to use the strict encoding detection or not. By default, it's false hence the trouble when using only
mb_detect_encoding( $recTitle, 'UTF-8' );
.http://php.net/manual/en/function.mb-detect-encoding.php
http://www.php.net/manual/en/function.mb-convert-encoding.php