Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@michael-cannon michael-cannon commented Jul 11, 2012

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