Text.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Luracast\Restler\Data;
  3. /**
  4. * Convenience class for String manipulation
  5. *
  6. * @category Framework
  7. * @package Restler
  8. * @author R.Arul Kumaran <arul@luracast.com>
  9. * @copyright 2010 Luracast
  10. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  11. * @link http://luracast.com/products/restler/
  12. *
  13. */
  14. class Text
  15. {
  16. /**
  17. * Given haystack contains the needle or not?
  18. *
  19. * @param string $haystack
  20. * @param string $needle
  21. * @param bool $caseSensitive
  22. *
  23. * @return bool
  24. */
  25. public static function contains($haystack, $needle, $caseSensitive = true)
  26. {
  27. if (empty($needle))
  28. return true;
  29. return $caseSensitive
  30. ? strpos($haystack, $needle) !== false
  31. : stripos($haystack, $needle) !== false;
  32. }
  33. /**
  34. * Given haystack begins with the needle or not?
  35. *
  36. * @param string $haystack
  37. * @param string $needle
  38. *
  39. * @return bool
  40. */
  41. public static function beginsWith($haystack, $needle)
  42. {
  43. $length = strlen($needle);
  44. return (substr($haystack, 0, $length) === $needle);
  45. }
  46. /**
  47. * Given haystack ends with the needle or not?
  48. *
  49. * @param string $haystack
  50. * @param string $needle
  51. *
  52. * @return bool
  53. */
  54. public static function endsWith($haystack, $needle)
  55. {
  56. $length = strlen($needle);
  57. if ($length == 0) {
  58. return true;
  59. }
  60. // @CHANGE LDR
  61. if (!is_string($haystack)) return false;
  62. return (substr($haystack, -$length) === $needle);
  63. }
  64. /**
  65. * Convert camelCased or underscored string in to a title
  66. *
  67. * @param string $name
  68. *
  69. * @return string
  70. */
  71. public static function title($name)
  72. {
  73. return
  74. ucwords(
  75. preg_replace(
  76. array('/(?<=[^A-Z])([A-Z])/', '/(?<=[^0-9])([0-9])/', '/([_-])/', '/[^a-zA-Z0-9\s]|\s\s+/'),
  77. array(' $0', ' $0', ' ', ' '),
  78. $name
  79. )
  80. );
  81. }
  82. /**
  83. * Convert given string to be used as a slug or css class
  84. *
  85. * @param string $name
  86. * @return string
  87. */
  88. public static function slug($name)
  89. {
  90. return preg_replace('/[^a-zA-Z]+/', '-', strtolower(strip_tags($name)));
  91. }
  92. }