| Newsgroups |
php.notes |
| Message-ID |
<[email protected]> |
Here is a shim for older php versions. It works for php 5.4 and above.
<?php
if (!function_exists('str_starts_with')) {
function str_starts_with($haystack, $needle) {
$haystackArray = str_split($haystack);
$needleArray = str_split($needle);
$matches = [];
foreach ($needleArray as $offset => $needleChar) {
if(!array_key_exists($offset, $haystackArray)){
break;
}
$haystackChar = $haystackArray[$offset];
if ($needleChar === $haystackChar) {
$matches[$offset] = $haystackChar;
continue;
}
if ($needleChar !== $haystackChar) {
break;
}
}
if (empty($matches)) {
return false;
}
return true;
}
}
$string = 'The lazy fox jumped over the fence';
if (str_starts_with($string, 'The')) {
echo "The string starts with 'The'\n";
}
----
Server IP: 206.189.2.9
Probable Submitter: 145.53.244.24
----
Manual Page -- https://php.net/manual/en/function.str-starts-with.php
Edit -- https://main.php.net/note/edit/130616
Del: integrated -- https://main.php.net/note/delete/130616/integrated
Del: useless -- https://main.php.net/note/delete/130616/useless
Del: bad code -- https://main.php.net/note/delete/130616/bad+code
Del: spam -- https://main.php.net/note/delete/130616/spam
Del: non-english -- https://main.php.net/note/delete/130616/non-english
Del: in docs -- https://main.php.net/note/delete/130616/in+docs
Del: other reasons-- https://main.php.net/note/delete/130616
Reject -- https://main.php.net/note/reject/130616
Search -- https://main.php.net/manage/user-notes.php