Simple way to implement this function in PHP 4<?phpif (function_exists('strripos') == false) { function strripos($haystack, $needle) { return strlen($haystack) - strpos(strrev($haystack), $needle); }}?>
(PHP 5, PHP 7, PHP 8)
strripos — 文字列中で、特定の(大文字小文字を区別しない)文字列が最後に現れた位置を探す
haystack
検索対象の文字列。
needle
検索する文字列。
PHP 8.0.0 より前のバージョンでは、needle
が文字列でない場合、
数値に変換され、文字の通常の値として扱われていました。
この振る舞いは PHP 7.3.0 以降では推奨されないので、
この機能を使用しないことを強く推奨します。
意図した動作に依存する場合、
needle
を string に明示的にキャストするか、
明示的に chr() 関数を呼び出すべきでしょう。
offset
ゼロまたは正の値の場合、
haystack
の
最初の offset
バイトをスキップし、
左から右に検索が行われます。
負の値の場合、
haystack
の
最後の offset
バイトをスキップし、
右から左に検索が行われ、
needle
が最初に現れる場所を探します。
注意:
この方が、最後の
offset
バイトより前にある、 最後のneedle
を効率的に探せます。
needle が見つかった位置を、
haystack
文字列の先頭 (offset の値とは無関係) からの相対位置で返します。
注意: 文字列の開始位置は 0 であり、1 ではありません。
needle が見つからない場合は false
を返します。
バージョン | 説明 |
---|---|
8.0.0 |
needle は、空文字列を受け入れるようになりました。
|
8.2.0 | ケースフォールディングは、setlocale() で設定されたロケールに依存しなくなりました。 ASCII のケースフォールディングのみが行われます。 ASCII でないバイト列は、バイト値として比較されます。 |
8.0.0 |
needle に数値を渡すことはサポートされなくなりました。
|
7.3.0 |
needle に数値を渡すことは非推奨になりました。
|
例1 単純な strripos() の例
<?php
$haystack = 'ababcd';
$needle = 'aB';
$pos = strripos($haystack, $needle);
if ($pos === false) {
echo "ごめんなさい、($needle) が ($haystack) の中に見つかりませんでした。";
} else {
echo "おめでとう!\n";
echo "($needle) が最後に ($haystack) に現れた位置は ($pos) です。";
}
?>
上の例の出力は以下となります。
おめでとう! (aB) が最後に (ababcd) に現れた位置は (2) です。
Simple way to implement this function in PHP 4<?phpif (function_exists('strripos') == false) { function strripos($haystack, $needle) { return strlen($haystack) - strpos(strrev($haystack), $needle); }}?>
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:<?phpfunction 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.
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:<?phpif(!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*/?>
OK, I guess this will be the final function implementation for PHP 4.x versions ( my previous posts are invalid )<?phpif(!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 */?>
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.
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:<?phpif (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.