PHP preg_match与ï

时间:2013-02-20 10:58:20

标签: php preg-match

如何使用preg_match接受所有普通字符,只接受i重音字符(ï)?

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

谢谢!

3 个答案:

答案 0 :(得分: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);

?>

答案 1 :(得分:1)

如果您要匹配unicode,则需要set the /u (unicode) flag,然后在您的范围内加入unicode字符。

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

有一个完整的unicode字符列表here

答案 2 :(得分:0)

使用unicode属性:

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

\pL代表任何语言的任何字母
\pN代表任何语言中的任何数字

相关问题