Check if a string ends with another string


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

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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.