2024 PHP Conference Japan

strrpos

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

strrpos尋找子字串在字串中最後一次出現的位置

說明

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

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

參數

haystack

要在其中搜尋的字串。

needle

要搜尋的字串。

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

偏移量 (offset)

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

如果為負數,則搜尋從 haystack 的右側開始 offset 個位元組,而不是從頭開始。搜尋從右至左執行,從選定的位元組開始搜尋 needle 的第一次出現。

注意事項:

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

回傳值

回傳 needle 出現在 haystack 字串中的位置(與搜尋方向或偏移量無關),位置從 haystack 字串的開頭算起。

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

如果找不到 needle,則回傳 false

警告

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

更新日誌

版本 說明
8.0.0 needle 現在接受空字串。
8.0.0 不再支援傳遞 整數 作為 needle
7.3.0 傳遞 整數 作為 needle 已被棄用。

範例

範例 #1 檢查 haystack 中是否存在 needle

很容易將回傳值誤認為「在位置 0 找到字元」和「找不到字元」。以下是如何區分它們的方法

<?php

$pos
= strrpos($mystring, "b");
if (
$pos === false) { // 注意:三個等號
// 找不到...
}

?>

範例 #2 使用偏移量搜尋

<?php
$foo
= "0123456789a123456789b123456789c";

// 從第 0 個位元組(從頭開始)尋找 '0'
var_dump(strrpos($foo, '0', 0));

// 從第 1 個位元組(在位元組 "0" 之後)尋找 '0'
var_dump(strrpos($foo, '0', 1));

// 從第 21 個位元組(在位元組 20 之後)尋找 '7'
var_dump(strrpos($foo, '7', 20));

// 從第 29 個位元組(在位元組 28 之後)尋找 '7'
var_dump(strrpos($foo, '7', 28));

// 從倒數第 5 個位元組開始,從右至左尋找 '7'
var_dump(strrpos($foo, '7', -5));

// 從倒數第 2 個位元組開始,從右至左尋找 'c'
var_dump(strrpos($foo, 'c', -2));

// 從倒數第 2 個位元組開始,從右至左尋找 '9c'
var_dump(strrpos($foo, '9c', -2));
?>

以上範例將輸出:

int(0)
bool(false)
int(27)
bool(false)
int(17)
bool(false)
int(29)

另請參閱:

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

新增註解

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

brian at enchanter dot net
17 年前
關於 'offset' 的文件說明有誤導性。

它說:「可以指定 offset 從字串中任意字元數開始搜尋。負值將會在字串結尾之前的任意點停止搜尋。」

如果您將 strrpos 想像成從字串結尾開始反向運作,這會令人感到困惑。

理解 offset 的更好方法是

- 如果 offset 為正數,則 strrpos 只會對從 offset 到結尾的字串部分進行操作。這通常會與未指定 offset 時產生相同的結果,除非 needle 的唯一出現位置在 offset 之前(在這種情況下,指定 offset 將找不到 needle)。

- 如果 offset 為負數,則 strrpos 只會對字串結尾的這些字元進行操作。如果 needle 離字串結尾更遠,則找不到它。

例如,如果您想在第 50 個字元之前找到字串中的最後一個空格,您需要執行以下操作

strrpos($text, " ", -(strlen($text) - 50));

如果您改用 strrpos($text, " ", 50),則您會在第 50 個字元和字串結尾之間找到最後一個空格,這可能不是您想要的。
dave at pixelmetrics dot com
5 年前
offset 的描述是錯誤的。以下是它的運作方式,以及支持的範例。

Offset 會影響搜尋的起點和終點。方向始終是從右到左。(描述錯誤地指出,當 offset 為正數時,PHP 會從左到右搜尋。)

以下是它的運作方式
當 offset 為正數時,PHP 會從 haystack 的結尾到 offset 從右到左搜尋。這會忽略 haystack 的左側。

當 offset 為負數時,PHP 會從右到左搜尋,從結尾的 offset 位元組開始,到 haystack 的開頭。這會忽略 haystack 的右側。

範例 1
$foo = ‘aaaaaaaaaa’;
var_dump(strrpos($foo, 'a', 5));
結果:int(10)

範例 2
$foo = "aaaaaa67890";
var_dump(strrpos($foo, 'a', 5));
var_dump(strrpos($foo, 'a', 5));

結果:int(5)

結論:當 offset 為正數時,PHP 會從 haystack 的結尾從右到左搜尋。
範例 3
var_dump(strrpos($foo, 'a', 5));
var_dump(strrpos($foo, '5', 5));

結果:bool(false)

結論:當 offset 為正數時,PHP 會在 offset 停止搜尋。
$foo = ‘aaaaaaaaaa’;
範例 4
var_dump(strrpos($foo, 'a', -5));

結果:int(6)

結論:當 offset 為負數時,PHP 會從右到左搜尋,從結尾的 offset 位元組開始。
$foo = "a234567890";

var_dump(strrpos($foo, 'a', -5));

結果:int(0)
範例 4
var_dump(strrpos($foo, 'a', -5));

結論:當 offset 為負數時,PHP 會從右到左搜尋,一直到 haystack 的開頭。

david dot mann at djmann dot co dot uk
6 年前
十年過去了,Brian 的筆記仍然很好地概述了偏移量的工作原理,但更簡短的摘要是:

strrpos($x, $y, 50); // 1: 這告訴 strrpos() 何時停止,從 $x 的開頭開始計數
strrpos($x, $y, -50); // 2: 這告訴 strrpos() 何時開始,從 $x 的結尾開始計數

換句話說,正數允許您搜尋字串的最右邊部分,而負數允許您搜尋字串的最左邊部分。

這兩種變體都很有用,但選擇錯誤的變體可能會導致一些非常令人困惑的結果!
Daniel Brinca
17 年前
以下是一個簡單的函數,用於查找 haystack 中下一個 needle 出現的位置,但向後搜尋(lastIndexOf 類型函數)

// 在 haystack 中向後搜尋 needle,並返回其位置
function rstrpos ($haystack, $needle, $offset){
$size = strlen ($haystack);
$pos = strpos (strrev($haystack), $needle, $size - $offset);

if ($pos === false)
return false;

return $size - $pos;
}

注意:支援完整的字串作為 needle
匿名
4 年前
$offset 的工作方式有很多令人困惑的地方,我覺得它其實很簡單。

如果 $offset 為正數,則操作會忽略 haystack 的前 $offset 個字元。
如果 $offset 為負數,則操作會忽略 haystack 的最後 $offset-1 個字元(別問為什麼是 -1...)。

要本能地理解這一點,只需想像將字元替換為無效的符號。以下是一個例子

<?php
$hackstack
= '0123456789';

// 搜尋整個序列 0123456789
var_dump(strrpos($hackstack, '0')); // int(0)
var_dump(strrpos($hackstack, '9')); // int(9)

// 只搜尋 ##23456789
var_dump(strrpos($hackstack, '1', 2)); // bool(false)
var_dump(strrpos($hackstack, '2', 2)); // int(2)

// 只搜尋 0123456###
var_dump(strrpos($hackstack, '6', -4)); // int(6)
var_dump(strrpos($hackstack, '7', -4)); // bool(false)

?>
stevewa
8 年前
我想在連字號「-」之前找到一個前導空格

原油(12 月) 51.00-56.00

所以我必須找到連字號的位置

然後從字串的長度中減去該位置(使其成為負數)
然後向左走到字串的開頭,尋找連字號之前的空格

例如
$str_position_hyphen = strpos($line_new,"-",$str_position_spread);

$line_new_length = strlen($line_new);

$str_position_hyphen_from_end = $str_position_hyphen - $line_new_length;

echo "從結尾算起的連字號位置 = " . $str_position_hyphen_from_end . "<br />\n";


$str_position_space_before_hyphen = strrpos($line_new, " ", $str_position_hyphen_from_end);

echo "*** 前一個空格位置 = " . $str_position_space_before_hyphen . "<br />\n";

$line_new = substr_replace($line_new, ",", $str_position_space_before_hyphen, 1 );

echo $line_new . "<br /><br />\n";
islandispeace at hotmail dot com
9 年前
$offset 的用法很容易造成誤解,以下是我個人的理解

function mystrrpos($haystack, $needle, $offset = 0) {
if ($offset == 0) {
return strrpos ($haystack, $needle);
} else {
return strrpos (substr($haystack, 0, $offset), $needle);
}
}
dmitry dot polushkin at gmail dot com
18 年前
傳回檔案名稱的字串副檔名,如果找不到副檔名,則傳回 false。
範例:filename_extension('some_file.mp3'); // mp3
比 pathinfo() 類似的功能快兩倍。
<?php
function filename_extension($filename) {
$pos = strrpos($filename, '.');
if(
$pos===false) {
return
false;
} else {
return
substr($filename, $pos+1);
}
}
?>
shimon at schoolportal dot co dot il
18 年前
在 PHP 4 的 strrstr 函式中也沒有偏移量。
<?
// by Shimon Doodkin
function chrrpos($haystack, $needle, $offset=false)
{
$needle=$needle[0];
$l=strlen($haystack);
if($l==0) return false;
if($offset===false) $offset=$l-1;
else
{
if($offset>$l) $offset=$l-1;
if($offset<0) return false;
}
for(;$offset>0;$offset--)
if($haystack[$offset]==$needle)
return $offset;
return false;
}
?>
gordon at kanazawa-gu dot ac dot jp
19 年前
這裡建議的「尋找字串最後一次出現位置」的函式不允許起始偏移量,所以這裡有一個經過測試的函式可以做到

function my_strrpos($haystack, $needle, $offset=0) {
// 與 strrpos 相同,除了 $needle 可以是一個字串
$strrpos = false;
if (is_string($haystack) && is_string($needle) && is_numeric($offset)) {
$strlen = strlen($haystack);
$strpos = strpos(strrev(substr($haystack, $offset)), strrev($needle));
if (is_numeric($strpos)) {
$strrpos = $strlen - $strpos - strlen($needle);
}
}
return $strrpos;
}
fab
19 年前
RE: hao2lian

此頁面上有很多 strrpos()(或稱 last_index_of)的替代實作,但很遺憾的是都有錯誤。這個是稍微修改下方其中一個版本,但它應該像一個*真正的* strrpos() 一樣運作,因為如果 haystack 中沒有 needle,它會傳回 false。

<?php

函式 my_strrpos($haystack, $needle) {
$index = strpos(strrev($haystack), strrev($needle));
如果(
$index === false) {
返回
false;
}
$index = strlen($haystack) - strlen($needle) - $index;
返回
$index;
}

?>
escii at hotmail dot com ( Brendan )
19 年前
當我發現 strrpos 的行為(它不應該被稱為 charrpos 嗎?)是這樣的時候,我馬上就不爽了,所以我做了自己的實作來搜尋字串。

<?
函式 proper_strrpos($haystack,$needle){
當($ret = strrpos($haystack,$needle))
{
如果(strncmp(substr($haystack,$ret,strlen($needle)),
$needle,strlen($needle)) == 0 )
返回 $ret;
$haystack = substr($haystack,0,$ret -1 );
}
返回 $ret;
}
?>
arlaud pierre
12 年前
這似乎表現得與 PHP 5 offset 參數在 PHP 4 版本中的行為完全相同。

<?php
function strrpos_handmade($haystack, $needle, $offset = 0){

if(
$offset === 0) return strrpos($haystack, $needle);

$length = strlen($haystack);
$size = strlen($needle);

if(
$offset < 0) {
$virtual_cut = $length+$offset;
$haystack = substr($haystack, 0, $virtual_cut+$size);
$ret = strrpos($haystack, $needle);
return
$ret > $virtual_cut ? false : $ret;
} else {
$haystack = substr($haystack, $offset);
$ret = strrpos($haystack, $needle);
return
$ret === false ? $ret : $ret+$offset;
}

}
?>
su.noseelg@naes, 只往後找
21 年前
也許只有我會在意這個問題,但段落最後一行只有一個單字真的讓我非常困擾。以下用一個例子來說明我不喜歡的地方:

The quick brown fox jumps over the lazy
dog.

所以我寫了這個函式。在任何包含兩個以上空格(也就是超過兩個單字)的段落中,它會將最後一個空格替換成「&nbsp;」。

<?php
function no_orphans($TheParagraph) {
if (
substr_count($TheParagraph," ") > 1) {
$lastspace = strrpos($TheParagraph," ");
$TheParagraph = substr_replace($TheParagraph,"&nbsp;",$lastspace,1);
}
return
$TheParagraph;
}
?>

如此,它會將「The quick brown fox jumps over the lazy dog.」改成「The quick brown fox jumps over the lazy&nbsp;dog.」。這樣,最後兩個字就會永遠在一起。
maxmike at gmail dot com
15 年前
我有一個執行反向 strpos 的簡單方法,可能會有用。這個版本我對偏移量的處理非常簡單
正偏移量從提供的字串索引向後搜尋。
負偏移量從字串末尾往前數指定字元數量的該字元位置向後搜尋。

以下是如何使用此函式逐步反向搜尋字串實例的範例

<?php
function backwardStrpos($haystack, $needle, $offset = 0){
$length = strlen($haystack);
$offset = ($offset > 0)?($length - $offset):abs($offset);
$pos = strpos(strrev($haystack), strrev($needle), $offset);
return (
$pos === false)?false:( $length - $pos - strlen($needle) );
}

$pos = 0;
$count = 0;
echo
"Test1<br/>";
while((
$pos = backwardStrpos("012340567890", "0", $pos)) !== false){
echo
$pos."<br/>";
$pos--;
if(
$pos < 0){
echo
"Done<br/>";break;
}
}
echo
"---===---<br/>\nTest2<br/>";
echo
backwardStrpos("12341234", "1", 2)."<br/>";
echo
backwardStrpos("12341234", "1", -2);
?>

輸出
Test1
11
5
0
Done
---===---
Test2
0
4

在 Test2 中,第一行從 "12341234" 中的第一個 3 開始檢查,並向後執行直到找到 1(位於位置 0)。

第二行從 "12341234" 中的第二個 2 開始檢查,並向開頭搜尋找到的第一個 1(位於位置 4)。

此函式適用於 php4,如果您也像我一樣對現有 strrpos 中的偏移量參數感到困惑,那麼它也很有用。
alexandre at NOSPAM dot pixeline dot be
15 年前
我需要檢查一個包含基於使用者輸入生成的資料夾名稱的變數是否具有尾斜線。

這個方法有效

<?php
// 偵測並移除尾端的斜線
$root_folder = ((strrpos($root_folder, '/') + 1) == strlen($root_folder)) ? substr($root_folder, 0, - 1) : $root_folder;
?>
FIE
21 年前
參考關於 lastIndexOf() 的註解和函式...
這個方法似乎對我無效,我能找到的唯一原因是,搜尋的字串(haystack)被反轉了,而目標字串(needle)沒有,因此它返回的是 haystack 的長度,而不是最後一個 needle 的位置... 我把它改寫如下:

<?php
function strlpos($f_haystack,$f_needle) {
$rev_str = strrev($f_needle);
$rev_hay = strrev($f_haystack);
$hay_len = strlen($f_haystack);
$ned_pos = strpos($rev_hay,$rev_str);
$result = $hay_len - $ned_pos - strlen($rev_str);
return
$result;
}
?>

這個函式遵循 strpos 的語法,而不是 Java 的 lastIndexOf。
我不確定在這裡分配所有這些變數是否會佔用更多資源,但如果您願意的話,您可以將它們全部放在 return 中,我不在乎我的伺服器是否崩潰 ;)

~寂靜的毀滅之風 呼嘯而過!
jafet at g dot m dot a dot i dot l dot com
17 年前
完整的 strpos() 功能,由小弟我本人提供。

<?php
function conforming_strrpos($haystack, $needle, $offset = 0)
{
# strpos() 為什麼要這樣做?總之...
if(!is_string($needle)) $needle = ord(intval($needle));
$haystack = strval($haystack);
# 參數
$hlen = strlen($haystack);
$nlen = strlen($needle);
# 拜託,這也是一個功能
if($nlen == 0)
{
trigger_error(__FUNCTION__.'(): 空的分隔符號。', E_USER_WARNING);
return
false;
}
$offset = intval($offset);
$hrev = strrev($haystack);
$nrev = strrev($needle);
# 搜尋
$pos = strpos($hrev, $nrev, $offset);
if(
$pos === false) return false;
else return
$hlen - $nlen - $pos;
}
?>

請注意,`$offset` 是從字串的結尾開始計算的。

另請注意,`conforming_strrpos()` 的執行速度大約比 `strpos()` 慢五倍。僅供參考。
dixonmd at gmail dot com
16 年前
<?php
$pos
= strlen(string $haystack) - strpos (strrev(string $haystack), strrev(string $needle)) - strlen(string $needle);
?>

如果 `needle` 中包含多個字元,那麼在 PHP 4 中,我們可以使用上述語句來尋找字串中子字串最後一次出現的位置,而不是使用 `strrpos()`。因為在 PHP 4 中,`strrpos()` 只會使用子字串的第一個字元。

例如
<?php
$haystack
= "you you you you you";
$needle = "you";
$pos1 = strlen($haystack) - strpos (strrev($haystack), strrev($needle)) - strlen($needle);
echo
$pos1 . "<br>";
$pos2 = strrpos($haystack, $needle);
echo
$pos2 . "<br>";
?>
php dot net at insite-out dot com
21 年前
我正在尋找 Java 的 lastIndexOf() 的 PHP 相對應函式。我找不到,所以我寫了這個

<?php
/*
用於傳回字串中最後一次出現子字串的位置的方法
*/
function last_index_of($sub_str,$instr) {
if(
strstr($instr,$sub_str)!="") {
return(
strlen($instr)-strpos(strrev($instr),$sub_str));
}
return(-
1);
}
?>

它會傳回您要搜尋的子字串的數字索引,如果字串中不存在該子字串,則傳回 -1。
mijsoot_at_gmail_dot_com
17 年前
首先,很抱歉我的英文不好。
我需要一個函式,可以給我一個字元倒數第二個出現的位置。
然後我告訴自己,最好寫一個可以給出倒數第 N 個位置的函式。

$return_context = "1173120681_0__0_0_Mijsoot_Thierry";

// 我需要在這裡找到 = "Mijsoot_Thierry"

//echo $return_context."<br />";// -- 除錯

function findPos($haystack,$needle,$position){
$pos = strrpos($haystack, $needle);
if($position>1){
$position --;
$haystack = substr($haystack, 0, $pos);
$pos = findPos($haystack,$needle,$position);
}else{
// echo $haystack."<br />"; // -- 除錯
return $pos;
}
return $pos;
}

var_dump(findPos($return_context,"_",2)); // -- 測試
tsa at medicine dot wisc dot edu
20 年前
管他呢,我想我應該再提供另一個函式。它不是很漂亮,但以下函式會從您的起始點開始倒數,並告訴您混合字元字串最後一次出現的位置

<?php
function strrposmixed ($haystack, $needle, $start=0) {
// 如果未設定,將 start 初始化為字串的結尾
if($start == 0) {
$start = strlen($haystack);
}

// 從 $start 開始反向搜尋
$currentStrPos=$start;
$lastFoundPos=false;

while(
$currentStrPos != 0) {
if(!(
strpos($haystack,$needle,$currentStrPos) === false)) {
$lastFoundPos=strpos($haystack,$needle,$currentStrPos);
break;
}
$currentStrPos--;
}

if(
$lastFoundPos === false) {
return
false;
} else {
return
$lastFoundPos;
}
}
?>
lee at 5ss dot net
21 年前
我應該先在這裡找找看的,但我卻寫了自己的 strrpos 版本,它支援搜尋整個字串,而不是單個字元。這是一個遞迴函式。我還沒有測試它是否比頁面上的其他函式更有效率。希望這能幫助到某些人!

<?php
//在 haystack 中尋找 needle 最後一次出現的位置
function str_rpos($haystack, $needle, $start = 0){
$tempPos = strpos($haystack, $needle, $start);
if(
$tempPos === false){
if(
$start == 0){
//字串中完全沒有 Needle
return false;
}else{
//找不到更多匹配項
return $start - strlen($needle);
}
}else{
//尋找下一個匹配項
return str_rpos($haystack, $needle, $tempPos + strlen($needle));
}
}
?>
kavih7 at yahoo dot com
18 年前
<?php
###################################################
#
# 說明:
# 此函式會傳回字串最後一次出現的位置,
# 而不是像 strrpos 只傳回單個字元最後一次出現的位置。
# 它也支援從 haystack 字串中開始搜尋的偏移量。
#
# 參數:
# $haystack (必填) -- 要搜尋的字串
# $needle (必填) -- 您要尋找的字串
# $offset (選填) -- 開始搜尋的偏移量
#
# 傳回值:
# 成功時傳回整數
# 若找不到字串則傳回 false
#
###################################################

function strrpos_string($haystack, $needle, $offset = 0)
{
if(
trim($haystack) != "" && trim($needle) != "" && $offset <= strlen($haystack))
{
$last_pos = $offset;
$found = false;
while((
$curr_pos = strpos($haystack, $needle, $last_pos)) !== false)
{
$found = true;
$last_pos = $curr_pos + 1;
}
if(
$found)
{
return
$last_pos - 1;
}
else
{
return
false;
}
}
else
{
return
false;
}
}
?>
Christ Off
17 年前
用於截斷字串的函式
移除句點和逗號
僅在找到字元 a 時才加上 ...

function TruncateString($phrase, $longueurMax = 150) {
$phrase = substr(trim($phrase), 0, $longueurMax);
$pos = strrpos($phrase, " ");
$phrase = substr($phrase, 0, $pos);
if ((substr($phrase,-1,1) == ",") or (substr($phrase,-1,1) == ".")) {
$phrase = substr($phrase,0,-1);
}
if ($pos === false) {
$phrase = $phrase;
}
else {
$phrase = $phrase . "...";
}
return $phrase;
}
jonas at jonasbjork dot net
19 年前
我需要從路徑中移除最後一個目錄,並想出了這個解決方案

<?php

$path_dir
= "/my/sweet/home/";
$path_up = substr( $path_dir, 0, strrpos( $path_dir, '/', -2 ) )."/";
echo
$path_up;

?>

或許對某些人有幫助..
nexman at playoutloud dot net
20 年前
類似 5.0 版本 strrpos 的 4.x 版本函式。
這將返回字串中 *最後一個* 出現的字串。

function strepos($haystack, $needle, $offset=0) {
$pos_rule = ($offset<0)?strlen($haystack)+($offset-1):$offset;
$last_pos = false; $first_run = true;
do {
$pos=strpos($haystack, $needle, (intval($last_pos)+(($first_run)?0:strlen($needle))));
if ($pos!==false && (($offset<0 && $pos <= $pos_rule)||$offset >= 0)) {
$last_pos = $pos;
} else { break; }
$first_run = false;
} while ($pos !== false);
if ($offset>0 && $last_pos<$pos_rule) { $last_pos = false; }
return $last_pos;
}

如果我的計算有誤,請隨時更正。
- 正偏移量將允許的第一個字元的最小字元索引位置。
- 負偏移量將從總長度中減去,而緊接在前的位址將是被搜尋的第一個字元的最大索引。

返回 needle 最後一次出現的字元索引 (0+)。

* 布林值 FALSE 將返回 haystack 中沒有匹配項,或超出偏移量指定的界限。
ZaraWebFX
21 年前
這可能是 derek 提到的

<?
function cut_last_occurence($string,$cut_off) {
return strrev(substr(strstr(strrev($string), strrev($cut_off)),strlen($cut_off)));
}

// 範例:切掉最後一次出現的 "limit"
$str = "select delta_limit1, delta_limit2, delta_limit3 from table limit 1,7";
$search = " limit";
echo $str."\n";
echo cut_last_occurence($str,"limit");
?>
php NO at SPAMMERS willfris SREMMAPS dot ON nl
17 年前
<?php
/*******
** 即使在 php4 中,這也可能是尋找字串最後一次出現的最短程式碼
*******/
function stringrpos($haystack,$needle,$offset=NULL)
{
return
strlen($haystack)
-
strpos( strrev($haystack) , strrev($needle) , $offset)
-
strlen($needle);
}
// @return -> 為便於閱讀而斷開。
?>
genetically altered mastermind at gmail
19 年前
取得檔案副檔名非常方便
$this->data['extension'] = substr($this->data['name'],strrpos($this->data['name'],'.')+1);
To Top