Answers for "php check string ends with"

PHP
7

php string ends with

function stringStartsWith($haystack,$needle,$case=true) {
    if ($case){
        return strpos($haystack, $needle, 0) === 0;
    }
    return stripos($haystack, $needle, 0) === 0;
}

function stringEndsWith($haystack,$needle,$case=true) {
    $expectedPosition = strlen($haystack) - strlen($needle);
    if ($case){
        return strrpos($haystack, $needle, 0) === $expectedPosition;
    }
    return strripos($haystack, $needle, 0) === $expectedPosition;
}
echo stringStartsWith("Hello World","Hell"); // true
echo stringEndsWith("Hello World","World"); // true
Posted by: Guest on August-06-2019
1

php check if string ends with

function startsWith( $haystack, $needle ) {
     $length = strlen( $needle );
     return substr( $haystack, 0, $length ) === $needle;
}
Posted by: Guest on September-20-2021

Browse Popular Code Answers by Language