php - 测试非 UTF-8 字符串

标签 php testing encoding

我已阅读 some其他 threads关于这个问题,但我不明白我做错了什么。

我有一个函数

public function reEncode($item)
{
    if (! mb_detect_encoding($item, 'utf-8', true)) {
        $item = utf8_encode($item);
    }

    return $item;
}

我正在为此编写测试。我想测试一个不是 UTF-8 的字符串,看看这个语句是否命中。我在创建测试字符串时遇到问题。

$contents = file_get_contents('CyrillicKOI8REncoded.txt');
var_dump(mb_detect_encoding($contents));

$sanitized = $this->reEncode($contents);
var_dump(mb_detect_encoding($sanitized));

最初我在一个文件上使用了 file_get_contents 我用各种编码在 sublime 中编码; Cyrillic (KOI8-R)HEXDOS (CP 437) 因为它已声明 file_get_contents() 忽略文件编码。这似乎是真的,因为返回的字符是一团乱麻。

就是说,每次我对这些变量使用 mb_detect_encoding() 时,我总是得到 ASCIIUTF-8。该语句永远不会被触发,因为 ASCIIUTF-8 的子集。

所以我尝试了 mb_convert_encoding()iconv() 将基本字符串转换为 UTF-16UTF- 32, base64, hex 等等 但每次 mb_detect_encoding() 返回 ASCIIUTF-8

在我的测试中,我想在调用此函数之前和之后断言编码类型。

$sanitized = $this->reEncode($contents);

$this->assertEquals('UTF-32', mb_detect_encoding($contents));
$this->assertEquals('UTF-8', mb_detect_encoding($sanitized));

我不明白我犯了什么基本错误,不断从 mb_detect_encoding() 返回 ASCIIUTF-8

最佳答案

好吧,事实证明你必须使用 strict 来检查,否则 mb_detect_encoding() 函数几乎没用。

$item = mb_convert_encoding('Котёнок', 'KOI8-R');

$sanitized = $this->reEncode($item);

$this->assertEquals('KOI8-R', mb_detect_encoding($item, 'KOI8-R', true));
$this->assertEquals('UTF-8', mb_detect_encoding($sanitised, 'UTF-8', true));

关于php - 测试非 UTF-8 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41985486/

相关文章:

php - 测试重复的返回值

php - 如何从类的方法中从 PDO 返回 LastInsertID

php - 确定 ORDER BY 语句中的顺序

testing - jmeter 在 Listener 中的工作顺序是什么?

objective-c - 如何在 Objective-C 中获取实例变量的名称?

php - cURL 不确定是否启用

testing - 当 mocha + enzyme + chai 包装在 Provider 组件中时,如何测试 react-native 组件

php - 使用 Composer 为项目运行所有 PHPUnit 测试

encoding - 如何在浏览器的文件下载框中显示非ascii文件名?

java - URLConnection URL 包含重音字符的编码问题