Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当第一个字符是数字时,更改为第一个大写字母_Php_Ucfirst - Fatal编程技术网

Php 当第一个字符是数字时,更改为第一个大写字母

Php 当第一个字符是数字时,更改为第一个大写字母,php,ucfirst,Php,Ucfirst,我以前也这样做过,但现在已经没有我写的东西了,也不记得以前是怎么写的了。在更正某些用户输入时,将“thisaitem”改为“thisaitem”,我当然会使用 ucfirst(strtolower($text)) 但是,当$text=“4温度控制”时,它没有任何用处 我确信我已对其进行了排序,因此“4个温度控制”是输出,但找不到ucfirst跳过非字母字符的引用 $text = "4 temperature controls"; $result = preg_replace_callbac

我以前也这样做过,但现在已经没有我写的东西了,也不记得以前是怎么写的了。在更正某些用户输入时,将“thisaitem”改为“thisaitem”,我当然会使用

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]));

?>

可能有更好的方法,但使用简单的
预匹配应该可以:

$text = "4 temperature controls";
$match = preg_match('/^([^a-zA-Z]*)(.*)$/', $text, $result);

$upper = ucfirst(mb_strtolower($result[2]));
echo $fixed = $result[1].$upper;

可能是一个正则表达式或一个split函数,请尝试使用:
$text=“php:a编程语言”。它将把
A
@hek2mgl小写,这是OP的意图,因为我发现这可能会更小心地处理输入字符串:。此外,它不需要降低整个字符串。(但可能不是必需的,只是一个提示)是的。但是-由于OP只询问了
ucfirst()
part,所以我没有涉及它(这就是为什么您在示例中看到小写的“A”)。无论如何,谢谢你的评论