我过去做过这个,但不再有我写的东西,也不记得我以前是怎么做的。在更正一些用户输入时,将“这是一个项目”说成“这是一个项目”,我当然使用
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()
根本不是这里的用例,因为它无法预测您的后续字符,它始终与第一个字符一起使用。
可能有更好的方法,但使用简单的方法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;
尝试这个:
<?php
$str = "4 temperature controls";
preg_match("~^(\d+)~", $str, $m);
$arr = explode($m[1],$str,2);
echo $m[1]." ".ucfirst(trim($arr[1]));
?>