/inc/api/helpers/string.php

https://github.com/jizillon/phpzillon-browser · PHP · 114 lines · 56 code · 7 blank · 51 comment · 7 complexity · 59d36552a4aa84e1f2c42a3754a3ccfd MD5 · raw file

  1. <?php
  2. /* Licensed under the Apache License, Version 2.0
  3. * See the LICENSE and NOTICE file for further information
  4. */
  5. /**
  6. * Static helper methods for string processing.
  7. */
  8. class api_helpers_string {
  9. /**
  10. * Escape string for using in JS values (apostrophe and newlines are
  11. * escaped)
  12. * @param $str string: String to escape.
  13. * @return string: Escaped value.
  14. */
  15. static function escapeJSValue($str) {
  16. return str_replace(
  17. array("'", "\n", "\r"),
  18. array("\\'", "\\n", "\\r"),
  19. $str);
  20. }
  21. /**
  22. * Remove control characters and convert all whitespace chars to
  23. * spaces. Also converts the string to UTF8.
  24. * Works recursively on arrays.
  25. * @param $str mixed: String or array to process.
  26. * @return mixed: Converted input value.
  27. */
  28. static function clearControlChars($str) {
  29. if (is_array($str)) {
  30. foreach($str as $key => $value) {
  31. $str[$key] = self::clearControlChars($value);
  32. }
  33. } else {
  34. $str = self::ensureUtf8($str);
  35. $str = str_replace(array("\t", "\n", "\r", NULL, "\x00"), " ", $str);
  36. $str = preg_replace('/\p{Cc}/u', '', $str);
  37. }
  38. return $str;
  39. }
  40. /**
  41. * Checks if the given string is UTF-8.
  42. * @param $str string: String to check.
  43. * @return bool: True if the string is encoded with UTF-8.
  44. * @see http://w3.org/International/questions/qa-forms-utf-8.html
  45. */
  46. public static function isUtf8($str) {
  47. return preg_match('%^(?:
  48. [\x09\x0A\x0D\x20-\x7E] # ASCII
  49. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  50. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  51. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  52. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  53. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  54. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  55. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  56. )*$%xs', $str);
  57. }
  58. /**
  59. * Takes a string which may be UTF-8 or ISO-8859-1/15 and returns the
  60. * UTF-8 encoded version.
  61. * @param $str string: String to convert.
  62. * @return string: Converted input string.
  63. */
  64. static function ensureUtf8($str) {
  65. if (self::isUtf8($str)) {
  66. return $str;
  67. } else {
  68. $str = iconv("ISO-8859-1", "UTF-8//ignore", $str);
  69. $str = preg_replace('/\p{Cc}/u', '', $str);
  70. return $str;
  71. }
  72. }
  73. /**
  74. * Remove all characters from the string except letters, decimal
  75. * digits, underlines and dashes.
  76. * @param $str string: String to clean.
  77. * @return string: Cleaned input string.
  78. */
  79. static function clean($str) {
  80. return preg_replace("/[^\w^\d^_^-]*/", "", $str);
  81. }
  82. /**
  83. * Replace double-slashes with one slash.
  84. * @param $str string: String to convert.
  85. * @return string: Converted string.
  86. */
  87. static function removeDoubleSlashes($str) {
  88. return preg_replace('#/{2,}#', '/', $str);
  89. }
  90. /**
  91. * Compares a wildcard string with an input string and returns returns
  92. * true if they match.
  93. *
  94. * @param $pattern string: Pattern to match against. May contain *
  95. * wildcards.
  96. * @param $input string: Input to compare against the pattern.
  97. */
  98. public static function matchWildcard($pattern, $input) {
  99. if (strpos($pattern, '*') !== false) {
  100. // Wildcard match
  101. $pattern = str_replace('\\*', '.*', preg_quote($pattern));
  102. return (preg_match('/' . $pattern . '/', $input) > 0);
  103. } else {
  104. return ($pattern == $input);
  105. }
  106. }
  107. }