1
0

is-utf-8.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. /**
  3. * dolphin. Collection of useful PHP skeletons.
  4. * Copyright (C) 2019 Johannes 'Banana' Keßler
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  8. *
  9. * You should have received a copy of the
  10. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  11. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  12. */
  13. /**
  14. * return if the given string is utf8
  15. * http://php.net/manual/en/function.mb-detect-encoding.php
  16. *
  17. * @param string $string
  18. * @return number
  19. */
  20. function is_utf8($string) {
  21. return preg_match('%^(?:
  22. [\x09\x0A\x0D\x20-\x7E] # ASCII
  23. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  24. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  25. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  26. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  27. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  28. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  29. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  30. )*$%xs', $string);
  31. }
  32. ?>