preg_match用于检查字符串中每个单词的首字母大写

lau*_*kok 0 php regex preg-match

我遇到了这个php ucfirst(),使大写字母成为字符串中每个单词的第一个字符.

$foo = 'hello world!';
$foo = ucfirst($foo); 
Run Code Online (Sandbox Code Playgroud)

但是如何使用正则表达式和preg_match()来检查,然后显示错误信息?

if (preg_match('/\b\p{Ll}/', $mem_titlename))
{
   $error = true;
   echo '<error elementid="mem_titlename" message="TITLE - please use uppercase for each word."/>';
}
Run Code Online (Sandbox Code Playgroud)

不知道上面的例子中那个表达式意味着什么,但我从某个地方得到它,它与ucfirst()做同样的工作......

Jas*_*ary 5

为什么要使用正则表达式?如果ucwords()做你想要的话似乎没必要.如果是这样,请执行以下操作:

if (ucwords($mem_titlename) == $mem_titlename) {
   $error = true;
   echo '<error elementid="mem_titlename" message="TITLE - please use uppercase for each word."/>';
}
Run Code Online (Sandbox Code Playgroud)

另请注意,ucwords()您所描述的不是ucfirst().http://www.php.net/manual/en/function.ucwords.php