note 53954 deleted from function.strpos by mazzanet
| From: | mazzanet@php.net | Date: | Sat, 18 Jun 2005 10:41:10 +0000 |
| Subject: | note 53954 deleted from function.strpos by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-90619@lists.php.net to get a copy of this message | ||
Note Submitter: keith dot ng at phpdiary dot org
Reason: useless
----
This function returns the position of the first occurence of a string in a string, but if you want
to find the *last* occurence instead, you could use PHP 5's strripos(). If you are using
PHP3/4 but require this functionality, here's a clever workaround that reverses the strings and
does not use any complicated loops.
/**
* Find position of last occurrence of a case-insensitive string in a string
*
* Returns the numeric position of the last occurrence of needle in the haystack string.
* NB: string positions start at 0, and not 1. If needle is not found, FALSE is returned.
*
* Author: Keith Ng (keith@phpdiary.org)
*/
function strripos($haystack, $needle)
{
$haystack = strtolower($haystack); //remove this for case-sensitivity
$needle = strtolower($needle); //remove this for case-sensitivity
$tmp = strpos(strrev($haystack),strrev($needle));
if ($tmp === false) {
return false;
} else {
return strlen($haystack) - ($tmp + strlen($needle)-1) - 1;
}
}