note 39957 added to function.strrchr

From: Date: Tue, 17 Feb 2004 00:16:20 +0000
Subject: note 39957 added to function.strrchr
Groups: php.notes 
Request: Send a blank email to php-notes+get-65242@lists.php.net to get a copy of this message
The function provided by marcokonopacki at hotmail dot com isn't really a reverse-version of strrchr(), rather a reverse version of strchr(). It returns everything from the start of $haystack up to the FIRST instance of the $needle. This is basically a reverse of the behavior which you expect from strchr(). A reverse version of strrchr() would return everything in $haystack up to the LAST instance of $needle, eg: <?php // reverse strrchr() - PHP v4.0b3 and above function reverse_strrchr($haystack, $needle) { $pos = strrpos($haystack, $needle); if($pos === false) { return $haystack; } return substr($haystack, 0, $pos + 1); } ?> Note that this function will need to be modified slightly to work with pre 4.0b3 versions of PHP due to the return type of strrpos() ('0' is not necessarily 'false'). Check the documentation on strrpos() for more info. A function like this can be useful for extracting the path to a script, for example: <? $string = "/path/to/the/file/filename.php"; echo reverse_strrchr($string, '/'); // will echo "/path/to/the/file/" ?> ---- Manual Page -- http://www.php.net/manual/en/function.strrchr.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39957 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39957&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39957&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65242) next »