PHP Conference Japan 2024

strrchr

(PHP 4, PHP 5, PHP 7, PHP 8)

strrchr尋找字串中最後一次出現的字元

描述

strrchr(字串 $haystack, 字串 $needle, 布林值 $before_needle = false): 字串|false

此函式會回傳 haystack 的一部分,該部分從最後一次出現的 needle 開始,直到 haystack 的結尾。

參數

haystack

要搜尋的字串

needle

如果 needle 包含多個字元,則只會使用第一個字元。此行為與 strstr() 的行為不同。

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

before_needle

如果為 true,則 strrchr() 會回傳 haystack 中最後一次出現 needle 之前的部分(不包括 needle)。

回傳值

如果找到 needle,此函式會回傳字串的一部分,否則回傳 false

更新日誌

版本 描述
8.3.0 新增了 before_needle 參數。
8.0.0 needle 現在接受空字串。
8.0.0 不再支援將 int 作為 needle 傳遞。
7.3.0 已棄用將 int 作為 needle 傳遞。

範例

範例 1 strrchr() 範例

<?php
$ext
= strrchr('somefile.txt', '.');
echo
"檔案副檔名:$ext \n";
$ext = $ext ? strtolower(substr($ext, 1)) : '';
echo
"檔案副檔名:$ext";
?>

以上範例將輸出類似以下內容

file extension: .txt
file extension: txt

注意事項

注意此函式是二進位安全的。

參見

  • strstr() - 尋找字串中第一次出現的字串
  • strrpos() - 尋找字串中最後一次出現的子字串的位置

新增註解

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

31
matthewkastor at live dot com
13 年前
<?php
/**
* 移除字串中相對於指定字元最後一次出現位置的前面或後面的部分。
* 可以保留或捨棄所選的字元。
*
* 使用範例:
* <code>
* $example = 'http://example.com/path/file.php';
* $cwd_relative[] = cut_string_using_last('/', $example, 'left', true);
* $cwd_relative[] = cut_string_using_last('/', $example, 'left', false);
* $cwd_relative[] = cut_string_using_last('/', $example, 'right', true);
* $cwd_relative[] = cut_string_using_last('/', $example, 'right', false);
* foreach($cwd_relative as $string) {
* echo "$string <br>".PHP_EOL;
* }
* </code>
*
* 輸出:
* <code>
* http://example.com/path/
* http://example.com/path
* /file.php
* file.php
* </code>
*
* @param string $character 要搜尋的字元。
* @param string $string 要搜尋的字串。
* @param string $side 決定要回傳字元左邊或右邊的文字。
* 選項為:left 或 right。
* @param bool $keep_character 決定是否保留字元。
* 選項為:true 或 false。
* @return string
*/
function cut_string_using_last($character, $string, $side, $keep_character=true) {
$offset = ($keep_character ? 1 : 0);
$whole_length = strlen($string);
$right_length = (strlen(strrchr($string, $character)) - 1);
$left_length = ($whole_length - $right_length - 1);
switch(
$side) {
case
'left':
$piece = substr($string, 0, ($left_length + $offset));
break;
case
'right':
$start = (0 - ($right_length + $offset));
$piece = substr($string, $start);
break;
default:
$piece = false;
break;
}
return(
$piece);
}
?>
12
sekati at gmail dot com
18 年前
只是對 carlos dot lage at gmail dot com 註解的一個小補充,使其更實用且更具彈性

<?php
// 傳回直到最後一個 needle 實例的所有內容
// 使用 $trail 來包含 needle 字元,包括最後一個 needle 及其之後的字元
function reverse_strrchr($haystack, $needle, $trail) {
return
strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
}
// 用法:
$ns = (reverse_strrchr($_SERVER["SCRIPT_URI"], "/", 0));
$ns2 = (reverse_strrchr($_SERVER["SCRIPT_URI"], "/", 1));
echo(
$ns . "<br>" . $ns2);
?>
6
dchris1 在 bigpond 點 net 點 au
20 年前
marcokonopacki 在 hotmail 點 com 提供的函式並不是 strrchr() 的真正反向版本,而是 strchr() 的反向版本。它會從 $haystack 的開頭傳回直到 $needle 的第一個實例的所有內容。這基本上是您期望從 strchr() 得到的行為的反向版本。strrchr() 的反向版本會傳回 $haystack 中直到 $needle 的最後一個實例的所有內容,例如

<?php
// 反向 strrchr() - PHP v4.0b3 及更高版本
function reverse_strrchr($haystack, $needle)
{
$pos = strrpos($haystack, $needle);
if(
$pos === false) {
return
$haystack;
}
return
substr($haystack, 0, $pos + 1);
}
?>

請注意,由於 strrpos() 的傳回類型('0' 不一定是 'false'),此函式需要稍微修改才能與 PHP 的 pre 4.0b3 版本一起使用。 請查看 strrpos() 的文件以取得更多資訊。

這樣的函式對於提取腳本的路徑很有用,例如

<?
$string = "/path/to/the/file/filename.php";

echo reverse_strrchr($string, '/'); // 將會輸出 "/path/to/the/file/"
?>
6
readless 在 gmx 點 net
18 年前
收件者:repley 在 freemail 點 it

程式碼運作良好,但當我試圖剪下腳本名稱時(例如:$_SERVER["SCRIPT_NAME"] => /index.php,在 "/" 處剪下字串並傳回 "index.php"),它沒有傳回任何內容 (false)。我修改了您的程式碼,現在即使 needle 是第一個字元也可以正常運作。
- 來自德國的問候

<?php
//strxchr(string haystack, string needle [, bool int leftinclusive [, bool int rightinclusive ]])
function strxchr($haystack, $needle, $l_inclusive = 0, $r_inclusive = 0){
if(
strrpos($haystack, $needle)){
// $haystack 中最後一個 $needle 之前的所有內容。
$left = substr($haystack, 0, strrpos($haystack, $needle) + $l_inclusive);

// 將 $r_inclusive 的值從 0 切換為 1,反之亦然。
$r_inclusive = ($r_inclusive == 0) ? 1 : 0;

// $haystack 中最後一個 $needle 之後的所有內容。
$right = substr(strrchr($haystack, $needle), $r_inclusive);

// 將 $left 和 $right 傳回陣列中。
return array($left, $right);
} else {
if(
strrchr($haystack, $needle)) return array('', substr(strrchr($haystack, $needle), $r_inclusive));
else return
false;
}
}
?>
4
freakinunreal 在 hotmail 點 com
18 年前
致:marcokonopacki 在 hotmail 點 com。

我必須對您的函式進行稍微修改,才能傳回完整的 needle(包含在內)。

// strrchr 的反向搜尋。
function strrrchr($haystack,$needle)
{

// 傳回 $needle 之前的所有內容(包含在內)。
//return substr($haystack,0,strpos($haystack,$needle)+1);
// 變成
return substr($haystack,0,strpos($haystack,$needle)+strlen($needle));
}

注意:+1 變成 +strlen($needle)

否則,它只會反向傳回 needle 中的第一個字元。
7
Primo Anderson Do S?tio
19 年前
$filename = 'strrchr_test.php';
print strrchr( $filename, '.' );

結果
.php

$other_filename = 'strrchr_test.asp.php';
print strrchr( $other_filename, '.' );

結果
.php
0
marcokonopacki 在 hotmail 點 com
21 年前
<?

// strrchr 的反向搜尋。
function strrrchr($haystack,$needle)
{

// 傳回 $needle 之前的所有內容(包含在內)。
return substr($haystack,0,strpos($haystack,$needle)+1);

}

$string = "FIELD NUMBER(9) NOT NULL";

echo strrrchr($string,")"); // 將會輸出 FIELD (9)

?>
-5
carlos 點 lage 在 gmail 點 com
19 年前
我使用了 dchris1 在 bigpond 點 net 點 au 的反向 strrchr,並將其簡化為一行程式碼並修復了其功能 - 如果找不到 needle,真正的 strrchr() 會傳回 FALSE,而不是 haystack :)

<?php
// 反向 strrchr()
function reverse_strrchr($haystack, $needle)
{
return
strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
}
?>
To Top