Posted By

Vordreller on 03/17/09


Tagged

String comparing


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

vali29


Check if a string ends with another string


 / Published in: PHP
 

This piece of code will enable you to check if a string ends with a certain string. Particularly handy when checking filetypes, like is something a jpg or not. The third parameter is optional, if set to true, than the comparison will be case specific.

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

Report this snippet  

You need to login to post a comment.