Skip to content

Instantly share code, notes, and snippets.

@michael-cannon
Created July 11, 2012 14:09
Show Gist options
  • Save michael-cannon/3090584 to your computer and use it in GitHub Desktop.
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
$recTitle = $record['title'];
$encoding = mb_detect_encoding( $recTitle, 'UTF-8', true );
if ( empty( $encoding ) ) {
$recTitle = mb_convert_encoding( $recTitle, 'UTF-8' );
}
@michael-cannon
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment