Posted By

Vordreller on 03/17/09


String comparing

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


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  


RSS Icon Subscribe to comments
Posted By: CarolineWebb on March 18, 2019

You need to login to post a comment.