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
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}
1function endsWith( $haystack, $needle ) {
2 $length = strlen( $needle );
3 if( !$length ) {
4 return true;
5 }
6 return substr( $haystack, -$length ) === $needle;
7}
8
1function startsWith( $haystack, $needle ) {
2 $length = strlen( $needle );
3 return substr( $haystack, 0, $length ) === $needle;
4}
5