note 18720 deleted from function.strrpos by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 09:00:09 +0000 |
| Subject: | note 18720 deleted from function.strrpos by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196809@lists.php.net to get a copy of this message | ||
Note Submitter: derek at slashview dot com
----
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. :)