我有一个小功能来纠正用户输入,如果他们没有在表单中大写他们的名字。
<pre>
function fixWords($x){
// define process
$x= strtolower($x);
$x= ucwords($x);
return $x;
}
</pre>
我发现双名和带连字符的名字没有大写。拆分 double 和带连字符的名称以强制大写的最佳方法是什么。谢谢。
最佳答案
您可以使用 ucfirst()和 preg_replace_callback() ,即:
function fixWords($name)
{
return preg_replace_callback('/\b\w/i', function($matches) {
return ucfirst(strtolower($matches[0]));
}, $name);
}
print fixWords("some name");
# Some Name
print fixWords("some-name");
# Some-Name
关于php - 解析用户在表单上的输入以强制在 php 中的名称上大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43748686/