2013-02-20 123 views
0

如何使用preg_match接受所有普通字符並只接受我重音字符(?)?PHP preg_match與ï

preg_match('#^[ a-zA-Z0-9\[\]()-.!?~*]+$#', $string); 

謝謝!

回答

1

如果你匹配你需要set the /u (unicode) flag的Unicode,然後在您的範圍Unicode字符。

preg_match('#^[ \x{00EF} a-z A-Z 0-9 \[\]()-.!?~*]+$#u', $string); 

有Unicode字符的完整列表here

+0

現在我有這個錯誤,因爲/ u:preg_match()[function.preg-match]:未知修飾符'/' – Jordy 2013-02-20 11:29:35

+1

嘗試刪除'/'以使其讀取'... $#u')。 ..' – darronz 2013-02-20 11:35:33

1

只需將其添加到現有的字符類的結尾......

<?php 

    // without the accented i  
    // returns 0, no match :(
    $string = "abcï"; 
    echo preg_match('#^[ a-zA-Z0-9\[\]()-.!?~*]+$#', $string); 

    // adding the accented i to the end of the character class 
    // returns 1, a match! 
    $string = "abcï"; 
    echo preg_match('#^[ a-zA-Z0-9\[\]()-.!?~*ï]+$#', $string); 

?> 
+0

這不工作:S – Jordy 2013-02-20 11:32:04

+1

也許您的文件以不能存儲utf-8的格式保存。確保你的php文件是用utf-8編碼保存的。這段代碼適用於我,所以我認爲問題在於代碼行之外。 – 2013-02-20 11:33:34

0

使用Unicode屬性:在任何語言

preg_match('#^[\pL\pN \[\]().!?~*-]+$#', $string); 

\pL代表任何語言的任何字母
\pN代表任意數目的