我需要验证一个字符串,以确保它只使用ASCII。是否有一个PHP函数会返回字符是否为ASCII?
您可以使用模式匹配preg_match()
。这会在字符串中搜索模式,如果匹配成功,则返回true,否则返回false。
在这个例子中,
$string = 'stack overflow Ã';
if(!preg_match( '/[''x80-''xff]+/',$string))
echo 'Valid ASCII';
else
echo 'Invalid ASCII';
来源:http://pageconfig.com/post/how-to-validate-ascii-text-in-php
if(preg_match('/[^'x20-'x7f]/', $string))