如何检测字符串中的非ASCII字符? [英] How do I detect non-ASCII characters in a string?
问题描述
如果我有一个PHP字符串,如何有效地确定它是否包含至少一个非ASCII字符?用非ASCII字符表示我不属于该表的任何字符, http://www.asciitable.com /,位置在32-126之间.
If I have a PHP string, how can I determine if it contains at least one non-ASCII character or not, in an efficient way? And by non-ASCII character, I mean any character that is not part of this table, http://www.asciitable.com/, positions 32 - 126 inclusive.
因此,它不仅必须是ASCII表的一部分,而且还必须是可打印的.我想检测一个字符串,其中包含至少一个不符合这些规范的字符(不可打印的ASCII字符或完全不同的字符,例如不属于该表的Unicode字符.
So not only does it have to be part of the ASCII table, but it also has to be printable. I want to detect a string that contains at least one character that does not meet these specifications (either non-printable ASCII, or a different character altogether, such as a Unicode character that is not part of that table.
推荐答案
我发现检测是否有任何字符不在列表内
I found it more useful to detect if any character falls out of the list
if(preg_match('/[^\x20-\x7e]/', $string))
这篇关于如何检测字符串中的非ASCII字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!