2010-11-10 160 views
0

我遇到了這個php ucfirst()使大寫字母中的每個單詞的第一個字符。preg_match檢查字符串中每個單詞的第一個字母的大寫

$foo = 'hello world!'; 
$foo = ucfirst($foo); 

但我該如何使用正則表達式和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."/>'; 
} 

不知道是什麼意思的表達在上面的例子,但我是從哪個地方做相同的工作的ucfirst()...

回答

5

爲什麼要使用正則表達式?看起來沒有必要如果ucwords()做你想要的。如果是這樣,就以下事項:

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

另外請注意,ucwords()做你的描述,而不是ucfirst()http://www.php.net/manual/en/function.ucwords.php

+1

+1。沒有必要使用正則表達式來完成這樣簡單的任務。 – You 2010-11-10 22:35:24

+0

明白了!非常感謝你! – laukok 2010-11-10 22:37:18

+0

@你,同意。雖然是一個很棒的工具,但不幸的是,我發現它們在錯誤的*實例中比在* *中使用得太頻繁。 – 2010-11-10 23:32:14

相關問題