note 68687 added to function.ucfirst
| From: | MarkusErnst at osu1 dot php dot net | Date: | Mon, 07 Aug 2006 10:20:05 +0000 |
| Subject: | note 68687 added to function.ucfirst | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115607@lists.php.net to get a copy of this message | ||
I encountered that using my_mb_ucfirst() as posted below distorts characters such as the right
single quote â (as typed with Alt-146 on Windows, or represented with ’ - I use it as
a typographically correct apostrophe) or the n-dash â (Alt-150 / –). It looks like
some or all non-latin1 characters get lost for utf8_encode(utf8_decode($string)).
Trying to work around this by replacing the first character only, and only if it was actually
changed to Uppercase, I came to the following:
<?php
function my_mb_ucfirst($str, $e='UTF-8') {
if (function_exists('mb_strtoupper')) {
$fc = mb_strtoupper(mb_substr($str, 0, 1, $e), $e);
return $fc.mb_substr($str, 1, mb_strlen($str, $e), $e);
}
else {
$latin1 = utf8_decode($str);
$first_lc = $latin1[0];
$first_uc = strtr($first_lc,
"abcdefghýijklmnopqrstuvwxyz".
"\x9C\x9A\xE0\xE1\xE2\xE3".
"\xE4\xE5\xE6\xE7\xE8\xE9".
"\xEA\xEB\xEC\xED\xEE\xEF".
"\xF0\xF1\xF2\xF3\xF4\xF5".
"\xF6\xF8\xF9\xFA\xFB\xFC".
"\xFE\xFF",
"ABCDEFGHÃIJKLMNOPQRSTUVWXYZ".
"\x8C\x8A\xC0\xC1\xC2\xC3\xC4".
"\xC5\xC6\xC7\xC8\xC9\xCA\xCB".
"\xCC\xCD\xCE\xCF\xD0\xD1\xD2".
"\xD3\xD4\xD5\xD6\xD8\xD9\xDA".
"\xDB\xDC\xDE\x9F");
if ($first_uc == $first_lc) return $str; // No change happened
else return utf8_encode($first_uc).substr($str, strlen(utf8_encode($first_lc)));
}
}
?>
----
Server IP: 194.153.94.10
Probable Submitter: 213.180.171.13
----
X-Spam-Status: Yes, hits=7.4 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HEAD_ILLEGAL_CHARS autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.ucfirst.php
Edit -- https://master.php.net/note/edit/68687
Del: integrated -- https://master.php.net/note/delete/68687/integrated
Del: useless -- https://master.php.net/note/delete/68687/useless
Del: bad code -- https://master.php.net/note/delete/68687/bad+code
Del: spam -- https://master.php.net/note/delete/68687/spam
Del: non-english -- https://master.php.net/note/delete/68687/non-english
Del: in docs -- https://master.php.net/note/delete/68687/in+docs
Del: other reasons-- https://master.php.net/note/delete/68687
Reject -- https://master.php.net/note/reject/68687
Search -- https://master.php.net/manage/user-notes.php