php check starts with string code example
Example 1: php string starts with
substr( $http, 0, 4 ) === "http";
substr( $https, 0, 5 ) === "https";
Example 2: 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");
echo stringEndsWith("Hello World","World");