note 53954 added to function.strpos

From: Date: Sat, 18 Jun 2005 10:25:02 +0000
Subject: note 53954 added to function.strpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-90618@lists.php.net to get a copy of this message
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; } } ---- Manual Page -- http://www.php.net/manual/en/function.strpos.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+53954 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53954&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53954&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53954&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53954&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+53954&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53954&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+53954&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#90618) next »