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.
strripos
(PHP 5)
strripos — 文字列中で、特定の(大文字小文字を区別しない)文字列が最後に現れた位置を探す
説明
int strripos
( string $haystack
, string $needle
[, int $offset
] )
文字列の中で、 大文字小文字を区別しないある文字列が最後に現れた位置を返します。 strrpos() と異なり、strripos() は大文字小文字を区別しません。
パラメータ
- haystack
-
検索を行う文字列。
- needle
-
needle は 1 文字あるいは複数の文字からなる 文字列であることに注意してください。
- offset
-
パラメータ offset を指定すると、 文字列中の任意の位置から検索を始めることができます。
負の offset 値を指定すると、文字列の最初 から数えて offset 文字目から検索を始めます。
返り値
needle が最後に現れた位置を返します。 文字列の位置は 0 から始まるのであって、 1 からではないことに注意してください。
needle が見つからない場合、FALSE が返されます。
例
例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) です。
strripos
dimmav at in dot gr
26-Sep-2008 01:31
26-Sep-2008 01:31
peev[dot]alexander at gmail dot com
21-Apr-2008 06:14
21-Apr-2008 06: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 = 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 */
?>
peev[dot]alexander at gmail dot com
23-Nov-2007 08:05
23-Nov-2007 08: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
18-Oct-2007 09:23
18-Oct-2007 09: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
03-Aug-2007 05:59
03-Aug-2007 05: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
03-Aug-2007 05:39
03-Aug-2007 05: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
04-Jul-2007 02:47
04-Jul-2007 02: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
31-May-2004 02:36
31-May-2004 02: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
