超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<strriposstrspn>
Last updated: Fri, 22 Jun 2007

strrpos

(PHP 3, PHP 4 , PHP 5)

strrpos --  Find position of last occurrence of a char in a string

Description

int strrpos ( string haystack, string needle [, int offset])

Returns the numeric position of the last occurrence of needle in the haystack string. Note that the needle in this case can only be a single character in PHP 4. If a string is passed as the needle, then only the first character of that string will be used.

If needle is not found, returns FALSE.

It is easy to mistake the return values for "character found at position 0" and "character not found". Here's how to detect the difference:

<?php

// in PHP 4.0b3 and newer:
$pos = strrpos($mystring, "b");
if (
$pos === false) { // note: three equal signs
   // not found...
}

// in versions older than 4.0b3:
$pos = strrpos($mystring, "b");
if (
is_bool($pos) && !$pos) {
  
// not found...
}
?>

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

注: As of PHP 5.0.0 offset may be specified to begin searching an arbitrary number of characters into the string. Negative values will stop searching at an arbitrary point prior to the end of the string.

注: The needle may be a string of more than one character as of PHP 5.0.0.

See also strpos(), strripos(), strrchr(), substr(), stristr(), and strstr().




add a note add a note User Contributed Notes
strrpos
derek at slashview dot com
02-Feb-2002 08:06
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.  :)

<strriposstrspn>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22