在PHP中检测非字母语言字符串 [英] Detect non-alphabetic language string in PHP
问题描述
我对此有一个类似的问题一个
I have a similar question to this one
但是,我的要求不太严格.
However, I have a less strict requirement.
我需要做的就是检测输入字符串是否包含任何非字母字符串.
All I need is to detect if the input string contains any non alphabetic string.
如果它确实包含非字母字符串,那么我将选择其他字体文件.
If it does contain non-alphabetic string, then I will select a different font file.
如果它仅包含字母字符串,那么我将选择一个字体文件,例如AmericanTypeWriter.
if it contains ONLY alphabetic string, then I will select a font file like AmericanTypeWriter.
通过字母字符串,其中包括各种可能的符号,例如逗号,标点符号等.
很难定义字母字符串.
让我定义什么是非字母字符串的例子.
Let me define what is an example of non-alphabetic string.
这是中文
并假定字符串为utf-8格式.
And assuming utf-8 format for the string.
另一种定义方式:任何不属于非欧洲语言字符的内容,都会自动假定为字母字符串.
我需要在php中进行此检测.
I need to do this detection in php by the way.
推荐答案
我找到了一个更好的答案.受到 https://stackoverflow.com/a/4923410/80353
I found a better answer. Inspired by https://stackoverflow.com/a/4923410/80353
header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding('utf-8');
function isThisChineseText($text) {
return preg_match("/\p{Han}+/u", $text);
}
这篇关于在PHP中检测非字母语言字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!