note 38996 added to function.strpos

From: Date: Wed, 14 Jan 2004 10:36:28 +0000
Subject: note 38996 added to function.strpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-63502@lists.php.net to get a copy of this message
Here's a (very) simple function to find the last occurence of a string in another one. I compared mine with some others which do the same job, and it seems that this one is a little bit faster because it avoids recursivity (multiple calls to strpos()). Hope this helps! <?php /* * @description: finds the last occurrence of * $needle in $haystack, * begining the search at the * $start-th character in $haystack * @returns: the start position of the * last occurence of $needle, * or false if $needle is not found */ function strlastpos($haystack, $needle, $start = 0){ $pos=strpos( strrev(substr($haystack,$start)), $needle===null?null:strrev($needle) //this is just to avoid errors when $needle = null (which is unlikely :-) ); if($pos===false) return false; else return strlen($haystack)-$pos-strlen($needle); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.strpos.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+38996 Delete -- http://master.php.net/manage/user-notes.php?action=delete+38996&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+38996&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#63502) next »