strripos

(PHP 5, PHP 7)

strripos문자열에서 대소문자 구분 없이 문자열의 마지막 위치를 찾습니다

설명

int strripos ( string $haystack , string $needle [, int $offset ] )

문자열에서 대소문자 구분 없는 문자열의 마지막 위치를 찾습니다. strrpos()와는 달리, strripos()는 대소문자를 구분하지 않습니다.

인수

haystack

탐색할 문자열

needle

needle은 문자열이나 하나 이상의 문자이여야 합니다.

offset

offset 인수를 지정하면 문자열의 해당 위치에서부터 탐색을 시작합니다.

음의 offset 값은 문자열 시작부터 offset 문자에서 탐색을 시작합니다.

반환값

needle의 마지막 위치를 반환합니다. 문자열 위치는 1이 아닌, 0에서 시작합니다.

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() - Return part of a string
  • stristr() - 대소문자를 구분하지 않는 strstr
  • strstr() - 문자열이 처음으로 나오는 부분을 찾습니다

add a note add a note

User Contributed Notes 7 notes

up
5
Yanik Lupien
16 years ago
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);
    }
}

?>
up
0
Anonymous
13 years ago
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.
up
0
peev[dot]alexander at gmail dot com
16 years ago
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 */
?>
up
-1
dimmav at in dot gr
15 years ago
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.
up
-1
peev[dot]alexander at gmail dot com
16 years ago
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*/
?>
up
-1
ElectroFox
16 years ago
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.
up
-7
admin at e-xxi dot net
14 years ago
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);
To Top