【获得英文字符串的首字母】
substr($str, 0, 1);
mb_substr($str, 0, 1, 'utf-8');
参考:https://stackoverflow.com/questions/1972100/getting-the-first-character-of-a-string-with-str0
参考:https://thisinterestsme.com/php-get-first-character-string/
【获得中文拼音字符串的首字母】
<?php function getFirstCharacter($str) { if (empty($str)) {return '';} $fchar = ord($str{0}); if ($fchar >= ord('A') && $fchar <= ord('z')) { return strtoupper($str{0}); } $s1 = iconv('UTF-8', 'gb2312', $str); $s2 = iconv('gb2312', 'UTF-8', $s1); $s = $s2 == $str ? $s1 : $str; $asc = ord($s{0}) * 256 + ord($s{1}) - 65536; switch ($asc) { case ($asc < -20319): return null; break; case ($asc <= -20284): return 'A'; break; case ($asc <= -19776): return 'B'; break; case ($asc <= -19219): return 'C'; break; case ($asc <= -18711): return 'D'; break; case ($asc <= -18527): return 'E'; break; case ($asc <= -18240): return 'F'; break; case ($asc <= -17923): return 'G'; break; case ($asc <= -17418): return 'H'; break; case ($asc <= -16475): return 'J'; break; case ($asc <= -16213): return 'K'; break; case ($asc <= -15641): return 'L'; break; case ($asc <= -15166): return 'M'; break; case ($asc <= -14923): return 'N'; break; case ($asc <= -14915): return 'O'; break; case ($asc <= -14631): return 'P'; break; case ($asc <= -14150): return 'Q'; break; case ($asc <= -14091): return 'R'; break; case ($asc <= -13319): return 'S'; break; case ($asc <= -12839): return 'T'; break; case ($asc <= -12557): return 'W'; break; case ($asc <= -11848): return 'X'; break; case ($asc <= -11056): return 'Y'; break; case ($asc <= -10247): return 'Z'; break; } } echo getFirstCharacter('?#哈哈哈!');
参考:https://blog.csdn.net/zhezhebie/article/details/77235243
参考:https://www.cnblogs.com/YFYQ/p/13220526.html
【获取英文姓名的首字母】
这段代码可以根据用户输入的英文姓名,分析出姓名的首字母输出,比如”Billy Bob” to “B.B.”
<?php function initials($name){ $nword = explode(" ",$name); foreach($nword as $letter){ $new_name .= $letter{0}.'.'; } return strtoupper($new_name); } echo initials('John Doe'); ?>