简体   繁体   中英

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. The 7-bit Ascii code contains every english character.

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(абв).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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