【发布时间】:2018-09-16 04:22:24
【问题描述】:
案例
似乎来自Intl 扩展的Spoofchecker 会产生误报:
<?php // 7.0 on linux
// File encoding of this script is UTF-8 (thus without BOM)
$sDefaultLocale = (new \Locale)->getDefault();
$oSpoofchecker = new \Spoofchecker;
$oSpoofchecker->setAllowedLocales($sDefaultLocale);
$sText = 'abc'; // US-ASCII
header('Content-Type: text/plain');
print
'Default locale: ' . $sDefaultLocale . PHP_EOL
. 'Byte length: ' . strlen($sText) . PHP_EOL // US-ASCII check
. 'Text "' . $sText . '" '
. ($oSpoofchecker->isSuspicious($sText, $sError) ? 'IS' : 'IS NOT')
. ' suspicious' . PHP_EOL
. 'Spoofchecker internal error information:' . PHP_EOL;
var_dump($sError);
结果
Default locale: en_US_POSIX
Byte length: 3
Text "abc" IS suspicious
Spoofchecker internal error information:
NULL
预期结果
Text "abc" IS NOT suspicious
这是因为 abc 是 US-ASCII,大概应该是 en_US_POSIX 的默认值。另外PHP Spoofchecker class 提到如果使用任何非英文字符,Spoofchecker::isSuspicious() 的返回码将是TRUE,这里不是这种情况。
可能的原因
documentation of Spoofchecker::setAllowedLocales() 目前几乎不存在,参数列表不包含可能值的列表。只能假设它必须与Locale 的兼容。文档内容如下:
区域设置使用 RFC 4646 语言标签(使用连字符,而不是下划线)标识
与Locale 使用下划线作为默认语言环境而不是连字符的测试结果相矛盾。但是当使用$oSpoofchecker->setAllowedLocales('en-US'); 运行另一个测试时,结果保持不变。
问题
如何正确使用Spoofchecker::isSuspicious()?
【问题讨论】:
-
如果您的文件编码是 UTF-8,为什么是 'abc' US-ASCII?你确定它是真的 UTF-8
-
US-ASCII 是 UTF-8 的子集。为了仔细检查,我用
$sText = chr(97) . chr(98) . chr(99);替换了$sText = 'abc';,不幸的是这并没有改变结果。我用 UTF-8 字符串在视觉上仔细检查了两者,并检查了 Eclipse 的设置。