If possible, using only standard PHP functions like substr(), strrpos(), strpos(), etc.
Simplest solution for this specific case is to use the offset parameter: $pos = strpos($info, '-', strpos($info, '-') + 1);
Python sequence, including list object allows indexing. Any element in list can be accessed using zero based index. If index is a negative number, count of index starts from end. As we want second to last element in list, use -2 as index.
A simple solution to find the last index of a character in a string is using the rfind() function, which returns the index of the last occurrence in the string where the character is found and returns -1 otherwise.
You can use any character you want. Just make sure it's unique and doesn't appear in the string already. FIND(“@”,SUBSTITUTE(A2,”/”,”@”,LEN(A2)-LEN(SUBSTITUTE(A2,”/”,””))),1) – This part of the formula would give you the position of the last forward slash.
First, find the last position:
$last = strrpos($haystack, $needle);
if ($last === false) {
return false;
}
From there, find the 2nd last:
$next_to_last = strrpos($haystack, $needle, $last - strlen($haystack) - 1);
General solution for any number of backwards steps:
function strrpos_count($haystack, $needle, $count)
{
if($count <= 0)
return false;
$len = strlen($haystack);
$pos = $len;
for($i = 0; $i < $count && $pos; $i++)
$pos = strrpos($haystack, $needle, $pos - $len - 1);
return $pos;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With