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 = 0 ){
return strpos( strtolower( $str ), strtolower( $needle ), $offset );
}/* endfunction stripos */
}/* endfunction exists stripos */
if(!function_exists("strripos")){
function strripos( $haystack, $needle, $offset = 0 ) {
if( !is_string( $needle ) )$needle = chr( intval( $needle ) );
if( $offset < 0 ){
$temp_cut = strrev( substr( $haystack, 0, abs($offset) ) );
}
else{
$temp_cut = strrev( substr( $haystack, 0, max( ( strlen($haystack) - $offset ), 0 ) ) );
}
if( ( $found = stripos( $temp_cut, strrev($needle) ) ) === FALSE )return FALSE;
$pos = ( strlen( $haystack ) - ( $found + $offset + strlen( $needle ) ) );
return $pos;
}/* endfunction strripos */
}/* endfunction exists strripos */
?>
strripos
(PHP 5)
strripos — 문자열에서 대소문자 구별 없이 문자열이 나타나는 마지막 위치를 찾습니다.
설명
int strripos
( string $haystack
, string $needle
[, int $offset
] )
haystack 문자열에서 마지막으로 needle 가 나타나는 숫자 위치를 반환합니다. strrpos()와는 달리, strripos()는 대소문자를 구별하지 않습니다. 문자열 위치는 1이 아닌, 0에서 시작하는 점에 주의하십시오.
needle 은 하나 이상의 문자를 가지는 문자열이여야 합니다.
needle 이 발견되지 않으면, FALSE를 반환합니다.
Warning
이 함수는 Boolean FALSE를 반환하지만, 0이나 "" 등의 FALSE로 취급할 수 있는 Boolean이 아닌 값을 반환할 수도 있습니다. Booleans 섹션에서 자세한 정보를 얻을 수 있습니다. 이 함수의 반환값을 테스트하기 위해서 === 연산자를 이용하십시오.
Example#1 간단한 strripos() 예제
<?php
$haystack = 'ababcd';
$needle = 'aB';
$pos = strripos($haystack, $needle);
if ($pos === false) {
echo "($haystack)에서 ($needle)를 찾지 못했습니다.";
} else {
echo "축하합니다!\n";
echo "($haystack)의 위치 ($pos)에서 마지막 ($needle)를 찾았습니다.";
}
?>
출력:
축하합니다! (ababcd)의 위치 (2)에서 마지막 (aB)를 찾았습니다.
offset 인수는 문자열에서 찾기 시작할 위치를 지정합니다. 음수는 문자열의 마지막으로부터 해당 위치에서 검색을 중단합니다.
strripos
peev[dot]alexander at gmail dot com
20-Apr-2008 02:14
20-Apr-2008 02:14
peev[dot]alexander at gmail dot com
23-Nov-2007 03:05
23-Nov-2007 03:05
Oops, I forgot to return "false" if the needle is not found. Here is the proper function.
<?php
if(!function_exists("strripos")){
function strripos($haystack, $needle, $offset=0) {
if($offset<0){
$temp_cut = strrev( substr( $haystack, 0, abs($offset) ) );
}
else{
$temp_cut = strrev( substr( $haystack, $offset ) );
}
$pos = strlen($haystack) - (strpos($temp_cut, strrev($needle)) + $offset + strlen($needle));
if ($pos == strlen($haystack)) { $pos = 0; }
if(strpos($temp_cut, strrev($needle))===false){
return false;
}
else return $pos;
}/* endfunction strripos*/
}/* endfunction exists strripos*/
?>
peev[dot]alexander at gmail dot com
17-Oct-2007 05:23
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 = strrev( substr( $haystack, 0, abs($offset) ) );
}
else{
$temp_cut = strrev( substr( $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
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.
ElectroFox
02-Aug-2007 01:39
02-Aug-2007 01:39
Actually, the above, "Simple way to implement this function in PHP 4" by Yanik Lupien, should be:
<?php
if (function_exists('strripos') == false) {
function strripos($haystack, $needle) {
return strlen($haystack) - strpos(strrev($haystack), strrev($needle));
}
}
?>
Note the reversal (<?php strrev($needle)?>) of the search string. This was left out in Yanik's example, and without it, you'll simply get the length of the haystack, as the forward string will not likely be found in the reversed haystack.
Thus; if we reverse the haystack, any instance of the search string ($needle) therein will also be reversed, so we must reverse it to look for it. :)
Yanik Lupien
03-Jul-2007 10:47
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);
}
}
?>
aidan at php dot net
30-May-2004 10:36
30-May-2004 10:36
This functionality is now implemented in the PEAR package PHP_Compat.
More information about using this function without upgrading your version of PHP can be found on the below link:
http://pear.php.net/package/PHP_Compat
