PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class.string.php

https://github.com/ilmich/clydephp
PHP | 113 lines | 83 code | 15 blank | 15 comment | 22 complexity | 2e142f8ff25d2a5eafb64abcb3e1856b MD5 | raw file
  1. <?php if (!defined('CLYDEPHP')) { header ('HTTP/1.1 404 Not Found'); exit(1); }
  2. class String {
  3. // Given a string such as "comment_123" or "id_57", it returns the final, numeric id.
  4. public static function splitId($str) {
  5. return self::match('/[_-]([0-9]+)$/', $str, 1);
  6. }
  7. // Creates a friendly URL slug from a string
  8. public static function slugify($str) {
  9. $str = preg_replace('/[^a-zA-Z0-9 -]/', '', $str);
  10. $str = strtolower(str_replace(' ', '-', trim($str)));
  11. $str = preg_replace('/-+/', '-', $str);
  12. return $str;
  13. }
  14. // Ensures $str ends with a single /
  15. public static function slash($str) {
  16. return rtrim($str, '/') . '/';
  17. }
  18. // Ensures $str DOES NOT end with a /
  19. public static function unslash($str) {
  20. return rtrim($str, '/');
  21. }
  22. // Returns the first $num words of $str
  23. public static function maxWords($str, $num, $suffix = '') {
  24. $words = explode(' ', $str);
  25. if(count($words) < $num)
  26. return $str;
  27. else
  28. return implode(' ', array_slice($words, 0, $num)) . $suffix;
  29. }
  30. // Quick wrapper for preg_match
  31. public static function match($regex, $str, $i = 0) {
  32. if(preg_match($regex, $str, $match) == 1)
  33. return $match[$i];
  34. else
  35. return false;
  36. }
  37. // Fixes MAGIC_QUOTES
  38. public static function fixSlashes($arr = '') {
  39. if(is_null($arr) || $arr == '')
  40. return null;
  41. if(!get_magic_quotes_gpc())
  42. return $arr;
  43. return is_array($arr) ? array_map(array('String','fixSlashes'), $arr) : stripslashes($arr);
  44. }
  45. // Formats a phone number as (xxx) xxx-xxxx or xxx-xxxx depending on the length.
  46. public static function format_phone($phone) {
  47. $phone = preg_replace("/[^0-9]/", '', $phone);
  48. if(strlen($phone) == 7)
  49. return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
  50. elseif(strlen($phone) == 10)
  51. return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
  52. else
  53. return $phone;
  54. }
  55. /**
  56. * Check if a string is encoded in utf-8
  57. *
  58. * from http://it.php.net/manual/en/function.mb-check-encoding.php#95289
  59. *
  60. * @param $str
  61. */
  62. public static function checkUtf8($str) {
  63. $len = strlen($str);
  64. for($i = 0; $i < $len; $i++){
  65. $c = ord($str[$i]);
  66. if ($c > 128) {
  67. if (($c > 247)) return false;
  68. elseif ($c > 239) $bytes = 4;
  69. elseif ($c > 223) $bytes = 3;
  70. elseif ($c > 191) $bytes = 2;
  71. else return false;
  72. if (($i + $bytes) > $len) return false;
  73. while ($bytes > 1) {
  74. $i++;
  75. $b = ord($str[$i]);
  76. if ($b < 128 || $b > 191) return false;
  77. $bytes--;
  78. }
  79. }
  80. }
  81. return true;
  82. }
  83. public static function isNullOrEmpty($str) {
  84. return is_null($str) || $str === '';
  85. }
  86. public static function nullToEmpty($str) {
  87. if (String::isNullOrEmpty($str)) {
  88. return '';
  89. }
  90. return $str;
  91. }
  92. public static function emptyToNull($str) {
  93. if (String::isNullOrEmpty($str)) {
  94. return null;
  95. }
  96. return $str;
  97. }
  98. }