Tag: Strings

Does a string begin with another string

Here is a function to check if a string begings with another string

  function startsWith($haystack,$needle,$case=true) {
    if($case)  { 
      return (strcmp(substr($haystack, 0, strlen($needle)),$needle)===0);
    }
    return (strcmp(strtolower(substr($haystack, 0, strlen($needle))),strtolower($needle))===0);
  }

Does a string end with another string

Here’s a nice function to check if a string ends with another string:

  function endsWith($haystack,$needle,$case=true) {
    if($case){
      return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);
    }
    return (strcmp(strtolower(substr($haystack, strlen($haystack) - strlen($needle))),strtolower($needle))===0);
  }