method lastIndexOf documentation in bbn\X
Searches from end to start
Example
X::lastIndexOf(['a', 'b', 'c', 'd'], 'c', 3);
// (int) 1
X::lastIndexOf('foobar', 'bar');
// (int) 3
X::lastIndexOf('foobar', 'bar', 4);
// (int) -1
X::lastIndexOf('foobarbar', 'bar');
// (int) 6
function($subject, $search, int $start = NULL)
{
$res = false;
if (is_array($subject)) {
$i = count($subject) - 1;
if ($i) {
if ($start > 0) {
if ($start > $i) {
return -1;
}
$i = $start;
}
elseif ($start < 0) {
$i -= $start;
if ($i < 0) {
return -1;
}
}
foreach ($subject as $s) {
if (($i <= $start) && ($s === $search)) {
$res = $i;
break;
}
else{
$i--;
}
}
}
}
elseif (is_string($subject)) {
if ($start > 0) {
$start = strlen($subject) - (strlen($subject) - $start);
}
$res = strrpos($subject, $search, $start);
}
return $res === false ? -1 : $res;
}
Searches from end to start BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2025
BBN Solutions