I had the task of creating the most approximate polyfill of str_starts_with() for the open CMS EffCore and the result was the following code:
namespace {
if (!function_exists('str_starts_with')) {
function str_starts_with($haystack, $needle) {
if (is_null ($haystack)) trigger_error('str_starts_with(): Passing null to parameter #1 ($haystack) of type string is deprecated in '.__FILE__, E_USER_DEPRECATED);
if (is_null ($needle) ) trigger_error('str_starts_with(): Passing null to parameter #2 ($needle) of type string is deprecated in '.__FILE__, E_USER_DEPRECATED);
if (is_array ($haystack)) throw new TypeError('str_starts_with(): Argument #1 ($haystack) must be of type string, array given');
if (is_object ($haystack)) throw new TypeError('str_starts_with(): Argument #1 ($haystack) must be of type string, object given');
if (is_resource($haystack)) throw new TypeError('str_starts_with(): Argument #1 ($haystack) must be of type string, resource given');
if (is_array ($needle) ) throw new TypeError('str_starts_with(): Argument #2 ($needle) must be of type string, array given');
if (is_object ($needle) ) throw new TypeError('str_starts_with(): Argument #2 ($needle) must be of type string, object given');
if (is_resource($needle) ) throw new TypeError('str_starts_with(): Argument #2 ($needle) must be of type string, resource given');
if ((string)$needle === '') return true;
return strpos((string)$haystack, (string)$needle) === 0;
}
}
}
Test data:
str_starts_with('100', '') # true
str_starts_with('100', '1') # true
str_starts_with('100', '0') # false
str_starts_with('100', 0) # false
str_starts_with('100', 1) # true
str_starts_with('100', 0.0) # false
str_starts_with('100', 1.0) # true
str_starts_with('100', true) # true / (string)true === '1'
str_starts_with('100', false) # true / (string)false === ''
str_starts_with('100', null) # Warning
str_starts_with('100', []) # Exception
str_starts_with('100', [0]) # Exception
str_starts_with('100', [1]) # Exception
str_starts_with('100', new stdCLass) # Exception
str_starts_with('100', new SomeClass) # Exception
str_starts_with('100', fopen('resource') # Exception
str_starts_with('010', '') # true
str_starts_with('010', '1') # false
str_starts_with('010', '0') # true
str_starts_with('010', 0) # true
str_starts_with('010', 1) # false
str_starts_with('010', 0.0) # true
str_starts_with('010', 1.0) # false
str_starts_with('010', true) # false / (string)true === '1'
str_starts_with('010', false) # true / (string)false === ''
str_starts_with('010', null) # Warning
str_starts_with('010', []) # Exception
str_starts_with('010', [0]) # Exception
str_starts_with('010', [1]) # Exception
str_starts_with('010', new stdCLass) # Exception
str_starts_with('010', new SomeClass) # Exception
str_starts_with('010', fopen('resource') # Exception
str_starts_with('', '1') # false
str_starts_with('100', '1') # true
str_starts_with('010', '1') # false
str_starts_with('001', '1') # false
str_starts_with(0, '1') # false
str_starts_with(1, '1') # true
str_starts_with(0.0, '1') # false
str_starts_with(1.0, '1') # true
str_starts_with(true, '1') # true / (string)true === '1'
str_starts_with(false, '1') # false / (string)false === ''
str_starts_with(null, '1') # Warning
str_starts_with([], '1') # Exception
str_starts_with([0], '1') # Exception
str_starts_with([1], '1') # Exception
str_starts_with(new stdCLass, '1') # Exception
str_starts_with(new SomeClass, '1') # Exception
str_starts_with(fofopen('resource'), '1') # Exception
s($str)->startsWith('|')
ands($str)->endsWith('}')
helpful, as found in this standalone library. – Adelaideadelajastr_starts_with
andstr_end_with
: https://mcmap.net/q/45126/-startswith-and-endswith-functions-in-php – Coan