简体   繁体   English

PHP输入过滤:仅英文字母

[英]PHP input filtering : Only English alphabet

What is the best way for check the input, if it contains any character from other languages. 如果输入中包含其他语言的任何字符,那么检查输入的最佳方法是什么。 (except english ) (英语除外)

if (preg_match("/[^\x00-\x7F]/",$string)) {
  // $string contains at least 
} else {
  // $string doesn't contain any foreign characters
}

This will check for any character that has ascii code higher than 127, because if it is higher, it's not in the english alphabet. 这将检查ASCII码高于127的任何字符,因为如果更高,则它不在英语字母中。 The 7-bit Ascii code contains every english character. 7位的Ascii代码包含每个英文字符。

ASCII表 source 资源

if this 如果这

preg_match("/[^\x00-\x7F]/",$string)

doesn't work (you get No ending delimiter '/' found ) then try this 不起作用(您没有找到结尾的定界符'/'),然后尝试执行此操作

preg_match('/[^\x00-\x7F]/',$string)
$input = 'abcабв';
$out = array();
preg_match_all(
    "|[a-zA-Z]?|",
    $input,
    $out
);

$out is going to contain all non-latin characters(абв). $ out将包含所有非拉丁字符(абв)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM