Posted By

Vordreller on 03/17/09


String comparison

Versions (?)

Who likes this?

2 people have marked this snippet as a favorite


Check if a string begins with another string

 / Published in: PHP

The counterpart of this snippet:

This one checks if a strings begins with another string. Again, third paramter is optional, if you don't set it, the comparison will be case-sensitive.

  1. function startsWith($haystack,$needle,$case=true) {
  2. if($case){return (strcmp(substr($haystack, 0, strlen($needle)),$needle)===0);}
  3. return (strcasecmp(substr($haystack, 0, strlen($needle)),$needle)===0);
  4. }

Report this snippet  

You need to login to post a comment.