<tutorialjinni.com/>

endsWith in PHP

Posted Under: PHP, Snippets, Tutorials on Aug 18, 2018
endsWith in PHP
PHP snippet function to check whether a string endsWith with a specified string or not. Function returns Boolean. This function can also handle case sensitivity. For startWith() function click here.
function endsWith($haystack, $needle,$ignoreCase=false)
{
    if($ignoreCase){
        $haystack= strtolower($haystack);
        $needle= strtolower($needle);
    }
    return (substr($haystack, strlen($haystack)-strlen($needle)) === $needle);
}
Sample execution of the above mentioned PHP function
// Case Sensitive testing
var_dump(endsWith("Ends With PHP function testing", "TestIng"));
// Case insensitive testing
var_dump(endsWith("Ends With PHP function testing", "testing",true));

Output

/var/www/html/tutorials/index.php:13:boolean false
/var/www/html/tutorials/index.php:14:boolean true


imgae