在 PHP 4 中實現此函式的簡單方法
<?php
if (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
應明確轉換為字串,或明確呼叫 chr() 函式。
offset
如果為零或正數,則從左到右執行搜尋,跳過 haystack
的前 offset
個位元組。
如果為負數,則從右到左執行搜尋,跳過 haystack
的最後 offset
個位元組,並搜尋 needle
的第一次出現。
注意:
這實際上是在最後
offset
個位元組之前尋找needle
的最後一次出現。
版本 | 說明 |
---|---|
8.2.0 | 大小寫轉換不再取決於使用 setlocale() 設定的地區設定。僅會執行 ASCII 大小寫轉換。非 ASCII 位元組將會根據其位元組值進行比較。 |
8.0.0 |
needle 現在接受空字串。 |
8.0.0 | 不再支援傳遞 int 作為 needle 。 |
7.3.0 | 傳遞 int 作為 needle 已被棄用。 |
範例 #1 一個簡單的 strripos() 範例
<?php
$haystack = 'ababcd';
$needle = 'aB';
$pos = strripos($haystack, $needle);
if ($pos === false) {
echo "很抱歉,在 ($haystack) 中找不到 ($needle)";
} else {
echo "恭喜!\n";
echo "我們在 ($haystack) 的位置 ($pos) 找到了最後一個 ($needle)";
}
?>
以上範例會輸出
Congratulations! We found the last (aB) in (ababcd) at position (2)
在 PHP 4 中實現此函式的簡單方法
<?php
if (function_exists('strripos') == false) {
function strripos($haystack, $needle) {
return strlen($haystack) - strpos(strrev($haystack), $needle);
}
}
?>
假設您只需要一個反向運作的 stripos 函式,並且期望 strripos 完成這項工作,您最好使用以下名為 strbipos 的自訂函式程式碼
<?php
function strbipos($haystack="", $needle="", $offset=0) {
// 從 $offset 開始,在 $haystack 中反向搜尋 $needle,並返回找到的位置或 false
$len = strlen($haystack);
$pos = stripos(strrev($haystack), strrev($needle), $len - $offset - 1);
return ( ($pos === false) ? false : $len - strlen($needle) - $pos );
}
// 測試
$body = "01234Xy7890XYz456xy90";
$str = "xY";
$len = strlen($body);
echo "測試 strbipos 中正值的偏移量<br>";
for ($i = 0; $i < $len; $i++) {
echo "在 [$body] 中搜尋 [$str],從偏移量 [$i] 開始: [" . strbipos($body, $str, $i) . "]<br>";
}
?>
請注意,此函式的功能與其說明完全相符,其結果與 PHP 5 的 strripos 函式不同。
我認為在搜尋 $needle 最後一次出現的「第一個位置」時,不應該低估 $needle 的長度。我改進了發佈的函式,增加了對偏移量的支援。我認為這與實際函式完全相同。
<?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 */
?>
好的,我想這將是 PHP 4.x 版本的最終函數實作(我之前的發文是無效的)
<?php
if( !function_exists("stripos")){
function stripos( $str, $needle, $offset = 0 ){
return strpos( strtolower( $str ), strtolower( $needle ), $offset );
}/* stripos 函式結束 */
}/* 判斷 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;
}/* strripos 函式結束 */
}/* 判斷 strripos 函式是否存在結束 */
?>
抱歉,我之前的回覆有點過於倉促。 這個簡單的 php4 版本還有一個問題,那就是如果找不到字串,它就會出錯。 它實際上應該看起來像這樣
<?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;
}
}
?>
注意,我們現在會檢查是否找到 $needle,如果沒有找到,則返回 0。