strripos

(PHP 5, PHP 7, PHP 8)

strripos计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)

说明

strripos ( string $haystack , string $needle , int $offset = 0 ) : int

以不区分大小写的方式查找指定字符串在目标字符串中最后一次出现的位置。与 strrpos() 不同,strripos() 不区分大小写。

参数

haystack

在此字符串中进行查找。

needle

注意 needle 可以是一个单字符或者多字符的字符串。

offset

参数 offset 可以被指定来查找字符串中任意长度的子字符串。

负数偏移量将使得查找从字符串的起始位置开始,到 offset 位置为止。

返回值

返回 needle 相对于 haystack 字符串的位置(和搜索的方向和偏移量无关)。同时注意字符串的起始位置为 0 而非 1。

如果 needle 未被发现,返回 false

Warning

此函数可能返回布尔值 false,但也可能返回等同于 false 的非布尔值。请阅读 布尔类型章节以获取更多信息。应使用 === 运算符来测试此函数的返回值。

范例

Example #1 strripos() 简单范例

<?php
$haystack 
'ababcd';
$needle   'aB';

$pos      strripos($haystack$needle);

if (
$pos === false) {
    echo 
"Sorry, we did not find ($needle) in ($haystack)";
} else {
    echo 
"Congratulations!\n";
    echo 
"We found the last ($needle) in ($haystack) at position ($pos)";
}
?>

以上例程会输出:

   Congratulations!
   We found the last (aB) in (ababcd) at position (2)

参见

  • strpos() - 查找字符串首次出现的位置
  • stripos() - 查找字符串首次出现的位置(不区分大小写)
  • strrchr() - 查找指定字符在字符串中的最后一次出现
  • substr() - 返回字符串的子串
  • stristr() - strstr 函数的忽略大小写版本
  • strstr() - 查找字符串的首次出现

User Contributed Notes

Anonymous 31-Aug-2010 01:05
Generally speaking, linear searches are from start to end, not end to start - which makes sense from a human perspective. If you need to find strings in a string backwards, reverse your haystack and needle rather than manually chopping it up.
admin at e-xxi dot net 08-Mar-2010 07:00
strripos() has very strange behaviour when you provide search position. For some reason it searches forward from the given position, instead of searching backward, that is more logical.

For example if you want to find instanse of $what, previous to the last, strripos($where, $what, $last_what_pos-1) will not wark as expected. It will return $last_what_pos again and again. And that has no sence at all.

To prevent this, I just used $prev_last_what_pos = strripos(substr($where,0,$last_what_pos), $what);
dimmav at in dot gr 25-Sep-2008 09:31
Suppose you just need a stripos function working backwards expecting that strripos does this job, you better use the following code of a custom function named strbipos:

<?php
function strbipos($haystack="", $needle="", $offset=0) {
// Search backwards in $haystack for $needle starting from $offset and return the position found or false

   
$len = strlen($haystack);
   
$pos = stripos(strrev($haystack), strrev($needle), $len - $offset - 1);
    return ( (
$pos === false) ? false : $len - strlen($needle) - $pos );
}

// Test
$body = "01234Xy7890XYz456xy90";
$str = "xY";
$len = strlen($body);
echo
"TEST POSITIVE offset VALUES IN strbipos<br>";
for (
$i = 0; $i < $len; $i++) {
    echo
"Search in [$body] for [$str] starting from offset [$i]: [" . strbipos($body, $str, $i) . "]<br>";
}
?>

Note that this function does exactly what it says and its results are different comparing to PHP 5 strripos function.
peev[dot]alexander at gmail dot com 20-Apr-2008 02:14
OK, I guess this will be the final function implementation for PHP 4.x versions ( my previous posts are invalid )

<?php

if(!function_exists("stripos")){
    function
stripos$str, $needle, $offset = ){
        return
strposstrtolower( $str ), strtolower( $needle ), $offset  );
    }
/* endfunction stripos */
}/* endfunction exists stripos */

if(!function_exists("strripos")){
    function
strripos$haystack, $needle, $offset = ) {
        if(  !
is_string( $needle )  )$needle = chrintval( $needle )  );
        if( 
$offset < ){
           
$temp_cut = strrevsubstr( $haystack, 0, abs($offset) )  );
        }
        else{
           
$temp_cut = strrev(    substr(   $haystack, 0, max(  ( strlen($haystack) - $offset ), )   )    );
        }
        if(   ( 
$found = stripos( $temp_cut, strrev($needle) )  ) === FALSE   )return FALSE;
       
$pos = (   strlen$haystack  ) - (  $found + $offset + strlen( $needle )  )   );
        return
$pos;
    }
/* endfunction strripos */
}/* endfunction exists strripos */
?>
peev[dot]alexander at gmail dot com 17-Oct-2007 05:23
I think you shouldn't underestimate the length of $needle in the search of THE FIRST POSITION of it's last occurrence in the string. I improved the posted function, with added support for offset. I think this is an exact copy of the real function:

<?php
if(!function_exists("strripos")){
    function
strripos($haystack, $needle, $offset=0) {
        if(
$offset<0){
           
$temp_cut = strrevsubstr( $haystack, 0, abs($offset) )  );
        }
        else{
           
$temp_cut = strrevsubstr( $haystack, $offset )  );
        }
       
$pos = strlen($haystack) - (strpos($temp_cut, strrev($needle)) + $offset + strlen($needle));
        if (
$pos == strlen($haystack)) { $pos = 0; }
        return
$pos;
    }
/* endfunction strripos*/
}/* endfunction exists strripos*/
?>
ElectroFox 02-Aug-2007 01:59
Sorry, I made that last post a bit prematurely.  One more thing wrong with the simple php4 version is that it breaks if the string is not found.  It should actually look like this:

<?php
if (function_exists('strripos') == false) {
    function
strripos($haystack, $needle) {
       
$pos = strlen($haystack) - strpos(strrev($haystack), strrev($needle));
        if (
$pos == strlen($haystack)) { $pos = 0; }
        return
$pos;
    }
}
?>

Note, we now check to see if the $needle was found, and if it isn't, we return 0.
Yanik Lupien 03-Jul-2007 10:47
Simple way to implement this function in PHP 4

<?php
if (function_exists('strripos') == false) {
    function
strripos($haystack, $needle) {
        return
strlen($haystack) - strpos(strrev($haystack), $needle);
    }
}

?>