本文目录一览:
php foreach输出数组只输出元素的第一个字符
foreach($list as $a)
$a 代表的是数组中的值。如果需要取得下标应该用
foreach($list as $key=$a)
$key 代表了数组中的下标。$a 代表的是数组的值
循环输出下标的语句应该是
foreach($list as $key=$a)
{
echo $key;
}
这样就是输出了下标。
php获取字符串首字母
他是根据汉字gb2312编码的头两个字符的范围来区分的。
识别不出很正常,因为gb2312本身包含的汉字就不全。你搜个utf8汉字转拼音的,你再修改下取个首字符。应该好点
[php教程]如何用PHP实现取字符的首字母?
?php
function getfirstchar($s0){
$c=ereg('[a-zA-Z]', strtoupper(substr( $s0, 0, 1 )));
if($c){
return strtoupper(substr( $s0, 0, 1 )) ;
}else{
if($fchar=ord("a") and $fchar=ord("Z") )return strtoupper($s0{0});
if(is_numeric(substr( $s0, 0, 1 ))){
$s0 =ToChinaseNum (substr( $s0, 0, 1 ));
}
$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 ToChinaseNum($num)
{
$char = array("零","一","二","三","四","五","六","七","八","九");
$dw = array("","十","百","千","万","亿","兆");
$retval = "";
$proZero = false;
for($i = 0;$i strlen($num);$i++)
{
if($i 0) $temp = (int)(($num % pow (10,$i+1)) / pow (10,$i));
else $temp = (int)($num % pow (10,1));
if($proZero == true $temp == 0) continue;
if($temp == 0) $proZero = true;
else $proZero = false;
if($proZero)
{
if($retval == "") continue;
$retval = $char[$temp].$retval;
}
else $retval = $char[$temp].$dw[$i].$retval;
}
if($retval == "一十") $retval = "十";
return $retval;
}
var_dump(getfirstchar("89ssss"));
?
php字符串首字母大写 如
执行效果:
源代码:
?php
$in="hello hello,hello-hello,hello/hello";
$out=preg_replace_callback('/([^a-zA-Z][a-z])/',
create_function(
'$m',
'return strtoupper($m[0]);'
),
ucfirst($in));
echo "$in\n$out\n";
?