gpt4 book ai didi

php - 使用 iconv() 检查无效的 UTF-8 字符 : Detected an illegal character in input string

转载 作者:行者123 更新时间:2023-12-04 02:17:32 25 4
gpt4 key购买 nike

我正在使用 iconv() 检查字符串是否包含无效的 UTF-8 字符。

$valid = $string == iconv('UTF-8', 'UTF-8//IGNORE', $string);

但是,这仍然会引发错误:“iconv():在输入字符串中检测到非法字符”

据我所知,这应该不可能使用//IGNORE 标志?

我在 Ubuntu 14.04.1 LTS 上使用 PHP 5.5.9-1ubuntu4.6

最佳答案

Another answericonv() 抛出错误的原因提供了更好的答案:

The output character set (the second parameter) should be different from the input character set (first param). If they are the same, then if there are illegal UTF-8 characters in the string, iconv will reject them as being illegal according to the input character set.

Taken from a comment在 PHP 手册中,您可以使用此函数检测字符串是否以 UTF-8 编码:

 $valid = mb_detect_encoding($str, 'UTF-8', true); // returns boolean.

更多info on mb_detect_encoding() ;

关于php - 使用 iconv() 检查无效的 UTF-8 字符 : Detected an illegal character in input string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33024486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com