String ends with and begins with


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2.  * Check if haystack string ends with needle string
  3.  *
  4.  * @param string $haystack
  5.  * @param string $needle
  6.  * @param boolean $caseSensitive (default true)
  7.  *
  8.  */
  9. function endsWith($haystack, $needle, $caseSensitive = true) {
  10. if ($caseSensitive) {
  11. return substr($haystack, -1 * strlen($needle)) == $needle;
  12. } else {
  13. return strtoupper(substr($haystack, -1 * strlen($needle))) == strtoupper($needle);
  14. }
  15. }
  16.  
  17. /**
  18.  * Check if haystack string begins with needle string
  19.  *
  20.  * @param string $haystack
  21.  * @param string $needle
  22.  * @param boolean $caseSensitive (default true)
  23.  *
  24.  */
  25. function beginsWith($haystack, $needle, $caseSensitive = true) {
  26. if ($caseSensitive) {
  27. return substr($haystack, 0, strlen($needle)) == $needle;
  28. } else {
  29. return strtoupper(substr($haystack, 0, strlen($needle))) == strtoupper($needle);
  30. }
  31. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.