Detect and convert utf-8 on string


/ Published in: PHP
Save to your folder(s)

I can't say I can rely on mb_detect_encoding(). Had some freaky false positives a while back. The following code will detect when you need to enconde or decode it.


Copy this code and paste it in your HTML
  1. if (preg_match('!!u', $string))
  2. {
  3. // this is utf-8
  4. }
  5. else
  6. {
  7. // definitely not utf-8
  8. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.