2024 年 PHP 日本研討會

strripos

(PHP 5, PHP 7, PHP 8)

strripos尋找字串中最後一次出現不區分大小寫子字串的位置

說明

strripos(字串 $haystack, 字串 $needle, 整數 $offset = 0): 整數|false

haystack 字串中尋找最後一次出現 needle 的數字位置。

strrpos() 不同,strripos() 不區分大小寫。

參數

haystack

要在其中搜尋的字串。

needle

要搜尋的字串。

在 PHP 8.0.0 之前,如果 needle 不是字串,它會被轉換為整數並作為字元的序數值。此行為自 PHP 7.3.0 起已被棄用,強烈不建議依賴此行為。根據預期行為,needle 應明確轉換為字串,或明確呼叫 chr() 函式。

offset

如果為零或正數,則從左到右執行搜尋,跳過 haystack 的前 offset 個位元組。

如果為負數,則從右到左執行搜尋,跳過 haystack 的最後 offset 個位元組,並搜尋 needle 的第一次出現。

注意:

這實際上是在最後 offset 個位元組之前尋找 needle 的最後一次出現。

傳回值

傳回 needle 相對於 haystack 字串開頭的位置(與搜尋方向或偏移量無關)。

注意 字串位置從 0 開始,而不是 1。

如果找不到 needle,則傳回 false

警告

此函式可能傳回布林值 false,但也可能傳回評估為 false 的非布林值。請閱讀 布林值 的章節以了解更多資訊。使用 === 運算子 測試此函式的傳回值。

更新日誌

版本 說明
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)

另請參閱

  • strpos() - 尋找字串中第一次出現子字串的位置
  • stripos() - 尋找字串中第一次出現子字串的位置(不區分大小寫)
  • strrpos() - 尋找字串中最後一次出現子字串的位置
  • strrchr() - 尋找字串中最後一次出現字元的位置
  • stristr() - 不區分大小寫的 strstr
  • substr() - 返回字串的一部分

新增註解

使用者貢獻的註解 6 則註解

Yanik Lupien
17 年前
在 PHP 4 中實現此函式的簡單方法

<?php
if (function_exists('strripos') == false) {
function
strripos($haystack, $needle) {
return
strlen($haystack) - strpos(strrev($haystack), $needle);
}
}

?>
dimmav at in dot gr
16 年前
假設您只需要一個反向運作的 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 函式不同。
peev[dot]alexander at gmail dot com
17 年前
我認為在搜尋 $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 */
?>
peev[dot]alexander at gmail dot com
16 年前
好的,我想這將是 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 函式是否存在結束 */
?>
匿名
14 年前
一般來說,線性搜尋是從頭到尾,而不是從尾到頭——從人類的角度來看,這才是合理的。如果您需要反向搜尋字串中的字串,請反轉您的 haystack 和 needle,而不是手動切割它。
ElectroFox
17 年前
抱歉,我之前的回覆有點過於倉促。 這個簡單的 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。
To Top