我以前也这样做过,但现在已经没有我写的东西了,也不记得以前是怎么写的了。在纠正一些用户输入时,比如说"这是一个项目"到"这是个项目",我当然会使用
ucfirst(strtolower($text))
然而,当$text="4个温度控制"时,它没有用
我确信我已经对它进行了排序,所以"4温度控制"是输出,但找不到ucfirst跳过非字母字符
使用正则表达式:
$text = "4 temperature controls";
$result = preg_replace_callback('/^([^a-z]*)([a-z])/', function($m)
{
return $m[1].strtoupper($m[2]);
}, strtolower($text));
ucfirst()
不是这里的用例,因为它不能预测下面的字符,所以它总是使用第一个字符。
试试这个:
<?php
$str = "4 temperature controls";
preg_match("~^('d+)~", $str, $m);
$arr = explode($m[1],$str,2);
echo $m[1]." ".ucfirst(trim($arr[1]));
?>
可能有更好的方法,但使用简单的preg_match
应该可以:
$text = "4 temperature controls";
$match = preg_match('/^([^a-zA-Z]*)(.*)$/', $text, $result);
$upper = ucfirst(mb_strtolower($result[2]));
echo $fixed = $result[1].$upper;