note 86863 modified in function.mb-convert-encoding by danbrown

From: Date: Fri, 07 Nov 2008 16:43:42 +0000
Subject: note 86863 modified in function.mb-convert-encoding by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-146288@lists.php.net to get a copy of this message
A common problem that people seem to be running into with mb_convert_encoding (and one that I had myself) is that it converts characters which are invalid for the target encoding into question marks (?), this is obviously not very useful when trying to present information to a user. A quick and dirty solution that produces results similar to iconv() is shown below: <?php function convert_to ( $source, $target_encoding ) { // detect the character encoding of the incoming file $encoding = mb_detect_encoding( $source, "auto" ); // escape all of the question marks so we can remove artifacts from // the unicode conversion process $target = str_replace( "?", "[question_mark]", $source ); // convert the string to the target encoding $target = mb_convert_encoding( $target, $target_encoding, $encoding); // remove any question marks that have been introduced because of illegal characters $target = str_replace( "?", "", $target ); // replace the token string "[question_mark]" with the symbol "?" $target = str_replace( "[question_mark]", "?", $target ); return $target; } ?> Hope this helps someone! --was-- A common problem that people seem to be running into with mb_convert_encoding (and one that I had myself) is that it converts characters which are invalid for the target encoding into question marks (?), this is obviously not very useful when trying to present information to a user. A quick and dirty solution that produces results similar to iconv() is shown below: <?php function convert_to ( $source, $target_encoding ) { // detect the character encoding of the incoming file $encoding = mb_detect_encoding( $file, "auto" ); // escape all of the question marks so we can remove artifacts from // the unicode conversion process $target = str_replace( "?", "[question_mark]", $source ); // convert the string to the target encoding $target = mb_convert_encoding( $target, $target_encoding, $encoding); // remove any question marks that have been introduced because of illegal characters $target = str_replace( "?", "", $target ); // replace the token string "[question_mark]" with the symbol "?" $target = str_replace( "[question_mark]", "?", $target ); return $target; } ?> Hope this helps someone! http://php.net/manual/en/function.mb-convert-encoding.php

« previous php.notes (#146288) next »