Answers for "php get part of string which ends"

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

Browse Popular Code Answers by Language