StringUtil.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace Sabre\VObject;
  3. /**
  4. * Useful utilities for working with various strings.
  5. *
  6. * @copyright Copyright (C) 2007-2014 fruux GmbH (https://fruux.com/).
  7. * @author Evert Pot (http://evertpot.com/)
  8. * @license http://sabre.io/license/ Modified BSD License
  9. */
  10. class StringUtil {
  11. /**
  12. * Returns true or false depending on if a string is valid UTF-8
  13. *
  14. * @param string $str
  15. * @return bool
  16. */
  17. static public function isUTF8($str) {
  18. // Control characters
  19. if (preg_match('%[\x00-\x08\x0B-\x0C\x0E\x0F]%', $str)) {
  20. return false;
  21. }
  22. return (bool)preg_match('%%u', $str);
  23. }
  24. /**
  25. * This method tries its best to convert the input string to UTF-8.
  26. *
  27. * Currently only ISO-5991-1 input and UTF-8 input is supported, but this
  28. * may be expanded upon if we receive other examples.
  29. *
  30. * @param string $str
  31. * @return string
  32. */
  33. static public function convertToUTF8($str) {
  34. $encoding = mb_detect_encoding($str , array('UTF-8','ISO-8859-1', 'WINDOWS-1252'), true);
  35. switch($encoding) {
  36. case 'ISO-8859-1' :
  37. $newStr = utf8_encode($str);
  38. break;
  39. /* Unreachable code. Not sure yet how we can improve this
  40. * situation.
  41. case 'WINDOWS-1252' :
  42. $newStr = iconv('cp1252', 'UTF-8', $str);
  43. break;
  44. */
  45. default :
  46. $newStr = $str;
  47. }
  48. // Removing any control characters
  49. return (preg_replace('%(?:[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F])%', '', $newStr));
  50. }
  51. }