Question
i know you can find the first and last occurrence in a string using strstr()
and strchr
but how do i find the second occurrence, and the third occurrence of needle inside haystack? im using this to find the last occurrence of needle and the first occurrence of another needle and their position, then return the string that is in between each. thank you.
Solution
You can use strpos()
and update the offset
value to be after whatever you just matched.
OTHER TIPS
You must find by strstr(), get result and start finding from result for time when strstr dont find nothing new.
You can use for it: http://php.net/manual/en/function.strpos.php
For the last occurrence, use strrpos
.
$lastPos = strrpos($haystack, $needle);
It takes an optional offset argument too.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow