如何使用 preg_match 接受所有普通字符,并且只接受 i 重音字符 (ï)?
preg_match('#^[ a-zA-Z0-9\[\]()-.!?~*]+$#', $string);
谢谢!
如何使用 preg_match 接受所有普通字符,并且只接受 i 重音字符 (ï)?
preg_match('#^[ a-zA-Z0-9\[\]()-.!?~*]+$#', $string);
谢谢!
只需将其添加到现有字符类的末尾...
<?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);
?>
如果您匹配 unicode,则需要设置 /u(unicode)标志,然后在您的范围内包含 unicode 字符。
 preg_match('#^[ \x{00EF} a-z A-Z 0-9 \[\]()-.!?~*]+$#u', $string);
这里有完整的 unicode 字符列表
使用 unicode 属性:
preg_match('#^[\pL\pN \[\]().!?~*-]+$#', $string);
\pL  代表任何语言的任何字母
\pN  代表任何语言的任何数字