note 86863 added to function.mb-convert-encoding
| From: | aaron at aarongough dot com | Date: | Thu, 06 Nov 2008 23:26:54 +0000 |
| Subject: | note 86863 added to function.mb-convert-encoding | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146264@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:
------------------------------------------------
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!
----
Server IP: 216.235.15.211
Probable Submitter: 99.231.148.142
----
Manual Page -- http://www.php.net/manual/en/function.mb-convert-encoding.php
Edit -- https://master.php.net/note/edit/86863
Del: integrated -- https://master.php.net/note/delete/86863/integrated
Del: useless -- https://master.php.net/note/delete/86863/useless
Del: bad code -- https://master.php.net/note/delete/86863/bad+code
Del: spam -- https://master.php.net/note/delete/86863/spam
Del: non-english -- https://master.php.net/note/delete/86863/non-english
Del: in docs -- https://master.php.net/note/delete/86863/in+docs
Del: other reasons-- https://master.php.net/note/delete/86863
Reject -- https://master.php.net/note/reject/86863
Search -- https://master.php.net/manage/user-notes.php