note 68687 deleted from function.ucfirst by derick

From: Date: Tue, 24 Apr 2007 14:56:16 +0000
Subject: note 68687 deleted from function.ucfirst by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-125831@lists.php.net to get a copy of this message
Note Submitter: Markus Ernst ---- 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 &#8217; - I use it as a typographically correct apostrophe) or the n-dash – (Alt-150 / &#8211;). 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))); } } ?>

« previous php.notes (#125831) next »