mb_detect_encoding detects ASCII as UTF-8?

后端 未结 3 1430
感情败类
感情败类 2020-12-18 05:15

I\'m trying to automatically convert imported IPTC metadata from images to UTF-8 for storage in a database based on the PHP mb_ functions.

Currently

相关标签:
3条回答
  • 2020-12-18 05:42

    If you do not want to worry about what encodings you will allow, you can add them all

    $encoding = mb_detect_encoding($val, implode(',', mb_list_encodings()));

    0 讨论(0)
  • 2020-12-18 05:52

    Specifying a custom order, where ASCII is detected first, works.

    mb_detect_encoding($val, 'ASCII,UTF-8,ISO-8859-15');
    

    For completeness, the list of available encodings is at http://www.php.net/manual/en/mbstring.supported-encodings.php

    0 讨论(0)
  • 2020-12-18 05:52

    You can specified explicitly

    $val = mb_convert_encoding($val, 'UTF-8', 'ASCII');
    

    EDIT:

    $val = mb_convert_encoding($val, 'UTF-8', 'auto');
    
    0 讨论(0)
提交回复
热议问题