Old PHP gem
I wrote this nugget of code back in Feb 2002. It got some really good reviews on the various PHP sites. :)
To find the position of the start of the last occurence of a string, we can do this:
$pos=strlen($haystack) -
(strpos(strrev($haystack), strrev($needle)) + strlen($needle));
The idea is to reverse both $needle and $haystack, use strpos to find the first occurence of $needle in $haystack, then count backwards by the length of $needle. Finally, subtract $pos from length of $haystack. A lot easier to figure out if you use a test string to visualize it. :)
0 Comments:
Post a Comment
<< Home