PHP Classes

Auto-Detect Character Encoding in PHP - Portable UTF-8 package blog

Recommend this page to a friend!
  All package blogs All package blogs   Portable UTF-8 Portable UTF-8   Blog Portable UTF-8 package blog   RSS 1.0 feed RSS 2.0 feed   Blog Auto-Detect Character...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Updated on: 2019-09-08

Posted on:

Viewers: 117 (September 2019 until May 2020)

Last month viewers: 5 (May 2020)

Package: Portable UTF-8

Mostly we are using UTF-8 encoding in our projects and it's working, but if you need to process third party data maybe with unknown encoding it can be painfully. So here comes a auto-detection for this kind of strings.




In "Portable UTF-8" I have implemented a auto-detect character encoding function with support for UTF-16 and UTF-32. So for example if your customer send you a mix of UTF-16 or UTF-8 or ISO ... encoded CSVs you can detect the encoding via:

UTF8::str_detect_encoding('中文空白'); // 'UTF-8'
UTF8::str_detect_encoding('Abc'); // 'ASCII'

You can also auto-convert the encoding and this function will also try to fix broken / double encoding, so you can call this function also on a UTF-8 string and you don't mess the string.

UTF8::encode('ISO-8859-1', '-ABC-中文空白-'); // '-ABC-????-'
//
UTF8::encode('UTF-8', '-ABC-中文空白-'); // '-ABC-中文空白-'
//
UTF8::encode('HTML', '-ABC-中文空白-'); // '-ABC-中文空白-'
//
UTF8::encode('BASE64', '-ABC-中文空白-'); // 'LUFCQy3kuK3mlofnqbrnmb0t'





You need to be a registered user or login to post a comment

1,558,869 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

No comments were submitted yet.



  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   Portable UTF-8 Portable UTF-8   Blog Portable UTF-8 package blog   RSS 1.0 feed RSS 2.0 feed   Blog Auto-Detect Character...  

For more information send a message to info at phpclasses dot org.