php string ends with

Solutions on MaxInterview for php string ends with by the best coders in the world

showing results for - "php string ends with"
Gaia
10 Aug 2017
1function stringStartsWith($haystack,$needle,$case=true) {
2    if ($case){
3        return strpos($haystack, $needle, 0) === 0;
4    }
5    return stripos($haystack, $needle, 0) === 0;
6}
7
8function stringEndsWith($haystack,$needle,$case=true) {
9    $expectedPosition = strlen($haystack) - strlen($needle);
10    if ($case){
11        return strrpos($haystack, $needle, 0) === $expectedPosition;
12    }
13    return strripos($haystack, $needle, 0) === $expectedPosition;
14}
15echo stringStartsWith("Hello World","Hell"); // true
16echo stringEndsWith("Hello World","World"); // true
Simon
16 Jun 2018
1function startsWith($haystack, $needle)
2{
3     $length = strlen($needle);
4     return (substr($haystack, 0, $length) === $needle);
5}
6
7function endsWith($haystack, $needle)
8{
9    $length = strlen($needle);
10    if ($length == 0) {
11        return true;
12    }
13
14    return (substr($haystack, -$length) === $needle);
15}