note 119137 added to function.iconv
| From: | DanielKlein | Date: | Fri, 08 Apr 2016 09:12:22 +0000 |
| Subject: | note 119137 added to function.iconv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205183@lists.php.net to get a copy of this message | ||
I just found out today that the Windows and *NIX versions of PHP use different iconv libraries and
are not very consistent with each other.
Here is a repost of my earlier code that now works on more systems. It converts as much as possible
and replaces the rest with question marks:
<?php
if (!function_exists('utf8_to_ascii')) {
setlocale(LC_CTYPE, 'en_AU.utf8');
if (@iconv("UTF-8", "ASCII//IGNORE//TRANSLIT", 'é') === false) {
// PHP is probably using the glibc library (*NIX)
function utf8_to_ascii($text) {
return iconv("UTF-8", "ASCII//TRANSLIT", $text);
}
}
else {
// PHP is probably using the libiconv library (Windows)
function utf8_to_ascii($text) {
if (is_string($text)) {
// Includes combinations of characters that present as a single glyph
$text = preg_replace_callback('/\X/u', __FUNCTION__, $text);
}
elseif (is_array($text) && count($text) == 1 && is_string($text[0])) {
// IGNORE characters that can't be TRANSLITerated to ASCII
$text = iconv("UTF-8", "ASCII//IGNORE//TRANSLIT", $text[0]);
// The documentation says that iconv() returns false on failure but it returns ''
if ($text === '' || !is_string($text)) {
$text = '?';
}
elseif (preg_match('/\w/', $text)) { // If the text contains any letters...
$text = preg_replace('/\W+/', '', $text); // ...then remove all
non-letters
}
}
else { // $text was not a string
$text = '';
}
return $text;
}
}
}
----
Server IP: 72.52.91.14
Probable Submitter: 211.30.51.139
----
Manual Page -- http://php.net/manual/en/function.iconv.php
Edit -- https://master.php.net/note/edit/119137
Del: integrated -- https://master.php.net/note/delete/119137/integrated
Del: useless -- https://master.php.net/note/delete/119137/useless
Del: bad code -- https://master.php.net/note/delete/119137/bad+code
Del: spam -- https://master.php.net/note/delete/119137/spam
Del: non-english -- https://master.php.net/note/delete/119137/non-english
Del: in docs -- https://master.php.net/note/delete/119137/in+docs
Del: other reasons-- https://master.php.net/note/delete/119137
Reject -- https://master.php.net/note/reject/119137
Search -- https://master.php.net/manage/user-notes.php