本文实例讲述了php实现字符串首字母大写和单词首字母大写的方法。分享给大家供大家参考。具体分析如下:
ucfirst可以对字符串首字母进行大小,ucwords可以对字符串中每个单词的首字母大写输出
<php
ucfirst("hello
world");
ucwords("iam
king
of
the
jungle");
>
希望本文所述对大家的php程序设计有所帮助。
<php
function getfirstchar($s0){
$fchar = ord($s0{0});
if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
$s1 = iconv("UTF-8","gb2312", $s0);
$s2 = iconv("gb2312","UTF-8", $s1);
if($s2 == $s0){$s = $s1;}else{$s = $s0;}
$asc = ord($s{0}) 256 + ord($s{1}) - 65536;
if($asc >= -20319 and $asc <= -20284) return "A";
if($asc >= -20283 and $asc <= -19776) return "B";
if($asc >= -19775 and $asc <= -19219) return "C";
if($asc >= -19218 and $asc <= -18711) return "D";
if($asc >= -18710 and $asc <= -18527) return "E";
if($asc >= -18526 and $asc <= -18240) return "F";
if($asc >= -18239 and $asc <= -17923) return "G";
if($asc >= -17922 and $asc <= -17418) return "H";
if($asc >= -17417 and $asc <= -16475) return "J";
if($asc >= -16474 and $asc <= -16213) return "K";
if($asc >= -16212 and $asc <= -15641) return "L";
if($asc >= -15640 and $asc <= -15166) return "M";
if($asc >= -15165 and $asc <= -14923) return "N";
if($asc >= -14922 and $asc <= -14915) return "O";
if($asc >= -14914 and $asc <= -14631) return "P";
if($asc >= -14630 and $asc <= -14150) return "Q";
if($asc >= -14149 and $asc <= -14091) return "R";
if($asc >= -14090 and $asc <= -13319) return "S";
if($asc >= -13318 and $asc <= -12839) return "T";
if($asc >= -12838 and $asc <= -12557) return "W";
if($asc >= -12556 and $asc <= -11848) return "X";
if($asc >= -11847 and $asc <= -11056) return "Y";
if($asc >= -11055 and $asc <= -10247) return "Z";
return null;
}
function make_semiangle($str){
$arr = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E', 'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J', 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O', 'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T', 'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y', 'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i', 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n', 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's', 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x', 'y' => 'y', 'z' => 'z', '(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '' => '[', '' => ']', '〖' => '[', '〗' => ']', '“' => '"', '”' => '"', '‘' => '\'', '’' => '\'', '{' => '{', '}' => '}', '《' => '<', '》' => '>', '%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-', ':' => ':', '。' => '', '、' => ',', ',' => ',', ';' => ';', '?' => '', '!' => '!', '…' => '', '‖' => '|', '|' => '|', '〃' => '"', ' ' => ' ');
return strtr($str, $arr);
}
function pinyin1($zh){
$zh = make_semiangle($zh);
$ret = "";
$s1 = iconv("UTF-8","gb2312", $zh);
$s2 = iconv("gb2312","UTF-8", $s1);
if($s2 == $zh){$zh = $s1;}
for($i = 0; $i < strlen($zh); $i++){
$s1 = substr($zh,$i,1);
$p = ord($s1);
if($p > 160){
$s2 = substr($zh,$i++,2);
$ret = getfirstchar($s2);
}else{
$ret = $s1;
}
}
return $ret;
}
//用法
echo pinyin1('中文获取首字母大写,‘支持’全角“的”转化!');
>
数字跟英文的全角被百度自动转了,如果需要下载附件源码
/
截取函数,中文一个,英文两个算一个
/
function mbsubstr($str, $start=0, $length, $suffix=true, $charset="utf-8") {
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$strArr = $match[0];
$str = array_values(tempStrArr($strArr));
$strArrCount = count($str);
if($strArrCount == $length+1){
$length += 1;
}
$slice = implode('',array_slice($str,$start,$length));
if($strArrCount > $length){
return $suffix $slice'' : $slice;
}else{
return $slice;
}
}
function tempStrArr($strArr){
$e = range('a','z');
$i = count($strArr);
foreach($strArr as $k=>$v){
if(in_array(strtolower($v), $e)){
if(in_array(strtolower($strArr[$k+1]), $e)){
$strArr[$k] = $v$strArr[$k+1];
unset($strArr[$k+1]);
break;
}
}elseif($v > 0 && $v < 10 && $strArr[$k+1] > 0 && $strArr[$k+1] < 10){
$strArr[$k] = $v$strArr[$k+1];
unset($strArr[$k+1]);
break;
}
$i--;
}
if($i > 0){
$strArr = tempStrArr($strArr);
}
return $strArr;
}
截取字符串, 中英文都可以
以上就是关于php实现字符串首字母大写和单词首字母大写的方法全部的内容,包括:php实现字符串首字母大写和单词首字母大写的方法、PHP 中文转拼音并首字母大写、php截取一段字符串中第一个出现的数字或字母或冒号前面的中文汉字的语句怎么写等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)