0

我自己“解决”了这个问题:文本文件必须是 ANSI 编码的。但我不知道为什么,或者为什么filter_var()没有帮助,所以我认为这对我自己和其他人仍然具有教育意义。

我还没有学习数据库,所以我开始涉足模板/为我的照片库生成的内容,从 txt 文件中提取信息。我有categories.txt

Architectural
Fashion
// etc

然后,在我的模板中:

$Categories = file('shared/categories.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$This_Page = filter_var($Categories[1], FILTER_SANITIZE_STRING);

$Image_List = file('shared/'.$This_Page.'.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach ($Categories as $c) {
    if ($c == $This_Page) {
        echo 'test successful!';
    }
    else {
        echo 'test failed';
    }
}

这总是有效的。但是,如果在第 2 行我使用Categories[0]

如果categories.txt是 UTF-8 编码,shared/$This_Page.txt则无法找到并if返回“测试失败”,并且var_dump()发现$This_Page比应有的长度长 3 个字符。即使与filter_var().

更糟糕的是,如果categories.txt是 Unicode 编码,则代码从第一行开始就失败,并显示“file() 期望参数 1 是有效路径”。

只有categories.txt编码为 ANSI 才能正常工作。

4

0 回答 0