1//php check if first four characters of a string = http
2substr( $http, 0, 4 ) === "http";
3//php check if first five characters of a string = https
4substr( $https, 0, 5 ) === "https";
5
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
1phpCopy<?php
2 $string = "Mr. Peter";
3 if(strncmp($string, "Mr.", 3) === 0){
4 echo "The string starts with the desired substring.";
5 }else
6 echo "The string does not start with the desired substring.";
7?>
8
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}
1phpCopy<?php
2 $string = "Mr. Peter";
3 if(substr($string, 0, 3) === "Mr."){
4 echo "The string starts with the desired substring.";
5 }else
6 echo "The string does not start with the desired substring.";
7?>
8
1phpCopy<?php
2 $string = "Mr. Peter";
3 if(strpos( $string, "Mr." ) === 0){
4 echo "The string starts with the desired substring.";
5 }else
6 echo "The string does not start with the desired substring.";
7?>
8