E_WARNING php encoding_error ai_generated true

Warning: mb_convert_encoding(): Illegal character encoding specified in /var/www/app/src/Utils/TextCleaner.php on line 15

ID: php/mbstring-illegal-character

Also available as: JSON · Markdown · 中文
90%Fix Rate
83%Confidence
1Evidence
2024-09-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.4 active
8.0 active
8.1 active
8.2 active
8.3 active

Root Cause

An invalid or unsupported encoding name is passed to mb_convert_encoding(), often due to a typo in the encoding string or using an alias not recognized by the mbstring extension.

generic

中文

传递给 mb_convert_encoding() 的编码名称无效或不支持,通常由编码字符串拼写错误或使用了 mbstring 扩展无法识别的别名引起。

Official Documentation

https://www.php.net/manual/en/function.mb-convert-encoding.php

Workarounds

  1. 95% success Validate the encoding name before conversion using mb_list_encodings(): $enc = 'utf-8'; if (in_array(strtolower($enc), array_map('strtolower', mb_list_encodings()))) { $result = mb_convert_encoding($string, 'UTF-8', $enc); } else { throw new InvalidArgumentException('Unsupported encoding'); }
    Validate the encoding name before conversion using mb_list_encodings(): $enc = 'utf-8'; if (in_array(strtolower($enc), array_map('strtolower', mb_list_encodings()))) { $result = mb_convert_encoding($string, 'UTF-8', $enc); } else { throw new InvalidArgumentException('Unsupported encoding'); }
  2. 85% success Use iconv() as a fallback for unsupported encodings: $result = @iconv($fromEncoding, 'UTF-8//IGNORE', $string);
    Use iconv() as a fallback for unsupported encodings: $result = @iconv($fromEncoding, 'UTF-8//IGNORE', $string);
  3. 90% success Correct the encoding name to a standard one recognized by mbstring, e.g., change 'utf8' to 'UTF-8', 'latin1' to 'ISO-8859-1'.
    Correct the encoding name to a standard one recognized by mbstring, e.g., change 'utf8' to 'UTF-8', 'latin1' to 'ISO-8859-1'.

中文步骤

  1. Validate the encoding name before conversion using mb_list_encodings(): $enc = 'utf-8'; if (in_array(strtolower($enc), array_map('strtolower', mb_list_encodings()))) { $result = mb_convert_encoding($string, 'UTF-8', $enc); } else { throw new InvalidArgumentException('Unsupported encoding'); }
  2. Use iconv() as a fallback for unsupported encodings: $result = @iconv($fromEncoding, 'UTF-8//IGNORE', $string);
  3. Correct the encoding name to a standard one recognized by mbstring, e.g., change 'utf8' to 'UTF-8', 'latin1' to 'ISO-8859-1'.

Dead Ends

Common approaches that don't work:

  1. 95% fail

    default_charset affects PHP's output encoding, not the input encoding parameter for mb_convert_encoding.

  2. 90% fail

    Suppressing warnings hides the problem but does not fix the encoding issue, potentially leading to corrupted data.