我使用iconv()檢查字符串是否包含無效的UTF-8字符。使用iconv()檢查無效的UTF-8字符:檢測到輸入字符串中的非法字符
$valid = $string == iconv('UTF-8', 'UTF-8//IGNORE', $string);
然而,這仍然引發錯誤:「的iconv():檢測到輸入字符串非法字符」
就我這方面的知識不應該使用//忽略標誌是可能的嗎?
我使用的是在Ubuntu 14.04.1 LTS PHP 5.5.9-1ubuntu4.6
我使用iconv()檢查字符串是否包含無效的UTF-8字符。使用iconv()檢查無效的UTF-8字符:檢測到輸入字符串中的非法字符
$valid = $string == iconv('UTF-8', 'UTF-8//IGNORE', $string);
然而,這仍然引發錯誤:「的iconv():檢測到輸入字符串非法字符」
就我這方面的知識不應該使用//忽略標誌是可能的嗎?
我使用的是在Ubuntu 14.04.1 LTS PHP 5.5.9-1ubuntu4.6
Another answer提供爲什麼iconv()
拋出一個錯誤一個更好的答案:在PHP
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手冊,可以檢測是否字符串在UTF-8編碼與此功能:
$valid = mb_detect_encoding($str, 'UTF-8', true); // returns boolean.
[iconv UTF-8 // IGNORE仍然會產生「非法字符」錯誤]的可能副本(http://stackoverflow.com/questions/9375909/iconv-utf-8-ignore-still-produces-illegal-character -error) – HPierce