|
|
strripos (PHP 5) strripos --
Возвращает позицию последнего вхождения подстроки без учета регистра
Описаниеint strripos ( string haystack, string needle [, int offset] )
Возвращает позицию последнего вхождения подстроки
needle в строку
haystack. В отличие от
strrpos(), эта функция не учитывает регистр
символов.
needle должен содержать не менее одного
символа.
Если подстрока needle не найдена,
возвращает FALSE.
| Внимание | Эта функция
может возвращать как логическое значение FALSE, так и не относящееся к логическому типу
значение, которое приводится к FALSE, например, 0 или
"". За более подробной информации обратитесь к разделу Булев тип. Используйте оператор === для проверки значения,
возвращаемого этой функцией. |
Пример 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) |
|
Необязательный аргумент offset позволяет
указать, с какого по счету символа строки
haystack начинать поиск. Отрицательное
значение предписывает прекратить поиск при достижении определенной
позиции до конца строки.
См. также описание функций strrpos(),
strrchr(),
substr(), stripos() и
stristr().
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 = 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;
}}?>
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.
ElectroFox
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
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);
}
}
?>
|