strrpos
(PHP 4, PHP 5, PHP 7, PHP 8)
strrpos — 文字列中に、ある部分文字列が最後に現れる場所を探す
説明
文字列 haystack
の中で、
needle
が最後に現れる位置を探します。
パラメータ
haystack
-
検索対象の文字列。
needle
-
検索する文字列。
PHP 8.0.0 より前のバージョンでは、
needle
が文字列でない場合、 数値に変換され、文字の通常の値として扱われていました。 この振る舞いは PHP 7.3.0 以降では推奨されないので、 この機能を使用しないことを強く推奨します。 意図した動作に依存する場合、needle
を string に明示的にキャストするか、 明示的に chr() 関数を呼び出すべきでしょう。 offset
-
ゼロまたは正の値の場合、
haystack
の 最初のoffset
バイトをスキップし、 左から右に検索が行われます。負の値の場合、
haystack
の 最初ではなく、最後からoffset
バイトの箇所より検索を開始します。 検索は右から左に行われ、 検索を開始した箇所からneedle
が最初に現れる場所を探します。注意:
この方が、最後の
offset
バイト、またはそれより前にある 最後のneedle
を効率的に探せます。
戻り値
needle が見つかった位置を、
haystack
文字列の先頭 (offset の値とは無関係) からの相対位置で返します。
注意: 文字列の開始位置は 0 であり、1 ではありません。
needle が見つからない場合は false
を返します。
変更履歴
バージョン | 説明 |
---|---|
8.0.0 |
needle は、空文字列を受け入れるようになりました。
|
8.0.0 |
needle に数値を渡すことはサポートされなくなりました。
|
7.3.0 |
needle に数値を渡すことは非推奨になりました。
|
例
例1 needle が haystack の中にあるかどうかの確認
"位置 0 に文字が見つかった" と "文字が見つからなかった" 場合の戻り値は混同しやすいです。この違いを見分ける方法を以下に示します。
<?php
$pos = strrpos($mystring, "b");
if ($pos === false) { // 注意: 等号が 3 つ並んでいます
// 見つからない...
}
?>
例2 オフセットつきの検索
<?php
$foo = "0123456789a123456789b123456789c";
// Looking for '0' from the 0th byte (from the beginning)
var_dump(strrpos($foo, '0', 0));
// Looking for '0' from the 1st byte (after byte "0")
var_dump(strrpos($foo, '0', 1));
// Looking for '7' from the 21th byte (after byte 20)
var_dump(strrpos($foo, '7', 20));
// Looking for '7' from the 29th byte (after byte 28)
var_dump(strrpos($foo, '7', 28));
// Looking for '7' right to left from the 5th byte from the end
var_dump(strrpos($foo, '7', -5));
// Looking for 'c' right to left from the 2nd byte from the end
var_dump(strrpos($foo, 'c', -2));
// Looking for '9c' right to left from the 2nd byte from the end
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() - 文字列の一部分を返す
User Contributed Notes 30 notes
The documentation for 'offset' is misleading.
It says, "offset may be specified to begin searching an arbitrary number of characters into the string. Negative values will stop searching at an arbitrary point prior to the end of the string."
This is confusing if you think of strrpos as starting at the end of the string and working backwards.
A better way to think of offset is:
- If offset is positive, then strrpos only operates on the part of the string from offset to the end. This will usually have the same results as not specifying an offset, unless the only occurences of needle are before offset (in which case specifying the offset won't find the needle).
- If offset is negative, then strrpos only operates on that many characters at the end of the string. If the needle is farther away from the end of the string, it won't be found.
If, for example, you want to find the last space in a string before the 50th character, you'll need to do something like this:
strrpos($text, " ", -(strlen($text) - 50));
If instead you used strrpos($text, " ", 50), then you would find the last space between the 50th character and the end of the string, which may not have been what you were intending.
The description of offset is wrong. Here’s how it works, with supporting examples.
Offset effects both the starting point and stopping point of the search. The direction is always right to left. (The description wrongly says PHP searches left to right when offset is positive.)
Here’s how it works:
When offset is positive, PHP searches right to left from the end of haystack to offset. This ignores the left side of haystack.
When offset is negative, PHP searches right to left, starting offset bytes from the end, to the start of haystack. This ignores the right side of haystack.
Example 1:
$foo = ‘aaaaaaaaaa’;
var_dump(strrpos($foo, 'a', 5));
Result: int(10)
Example 2:
$foo = "aaaaaa67890";
var_dump(strrpos($foo, 'a', 5));
Result: int(5)
Conclusion: When offset is positive, PHP searches right to left from the end of haystack.
Example 3:
$foo = "aaaaa567890";
var_dump(strrpos($foo, 'a', 5));
Result: bool(false)
Conclusion: When offset is positive, PHP stops searching at offset.
Example 4:
$foo = ‘aaaaaaaaaa’;
var_dump(strrpos($foo, 'a', -5));
Result: int(6)
Conclusion: When offset is negative, PHP searches right to left, starting offset bytes from the end.
Example 5:
$foo = "a234567890";
var_dump(strrpos($foo, 'a', -5));
Result: int(0)
Conclusion: When offset is negative, PHP searches right to left, all the way to the start of haystack.
Ten years on, Brian's note is still a good overview of how offsets work, but a shorter and simpler summary is:
strrpos($x, $y, 50); // 1: this tells strrpos() when to STOP, counting from the START of $x
strrpos($x, $y, -50); // 2: this tells strrpos() when to START, counting from the END of $x
Or to put it another way, a positive number lets you search the rightmost section of the string, while a negative number lets you search the leftmost section of the string.
Both these variations are useful, but picking the wrong one can cause some highly confusing results!
Here is a simple function to find the position of the next occurrence of needle in haystack, but searching backwards (lastIndexOf type function):
//search backwards for needle in haystack, and return its position
function rstrpos ($haystack, $needle, $offset){
$size = strlen ($haystack);
$pos = strpos (strrev($haystack), $needle, $size - $offset);
if ($pos === false)
return false;
return $size - $pos;
}
Note: supports full strings as needle
There is a lot of confusion around how $offset works and I feel it's really quite simple.
If $offset is positive, the operation ignores the first $offset characters of the haystack.
If $offset is negative, the operation ignores the last $offset-1 characters of the haystack (don't ask why -1...).
To understand this instinctively, just imagine the characters being replaced with invalid symbols. Here's an example:
<?php
$hackstack = '0123456789';
// Search the entire sequence 0123456789
var_dump(strrpos($hackstack, '0')); // int(0)
var_dump(strrpos($hackstack, '9')); // int(9)
// Only search ##23456789
var_dump(strrpos($hackstack, '1', 2)); // bool(false)
var_dump(strrpos($hackstack, '2', 2)); // int(2)
// Only search 0123456###
var_dump(strrpos($hackstack, '6', -4)); // int(6)
var_dump(strrpos($hackstack, '7', -4)); // bool(false)
?>
i wanted to find a leading space BEFORE a hyphen
Crude Oil (Dec) 51.00-56.00
so I had to find the position of the hyphen
then subtract that position from the length of the string (to make it a negative number)
and then walk left toward the beginning of the string, looking for the first space before the hyphen
ex:
$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 "hyphen position from end = " . $str_position_hyphen_from_end . "<br />\n";
$str_position_space_before_hyphen = strrpos($line_new, " ", $str_position_hyphen_from_end);
echo "*** previous space= " . $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";
$offset is very misleading, here is my understanding:
function mystrrpos($haystack, $needle, $offset = 0) {
if ($offset == 0) {
return strrpos ($haystack, $needle);
} else {
return strrpos (substr($haystack, 0, $offset), $needle);
}
}
Returns the filename's string extension, else if no extension found returns false.
Example: filename_extension('some_file.mp3'); // mp3
Faster than the pathinfo() analogue in two times.
<?php
function filename_extension($filename) {
$pos = strrpos($filename, '.');
if($pos===false) {
return false;
} else {
return substr($filename, $pos+1);
}
}
?>
In strrstr function in php 4 there is also no offset.
<?
// 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;
}
?>
The "find-last-occurrence-of-a-string" functions suggested here do not allow for a starting offset, so here's one, tried and tested, that does:
function my_strrpos($haystack, $needle, $offset=0) {
// same as strrpos, except $needle can be a string
$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;
}
RE: hao2lian
There are a lot of alternative - and unfortunately buggy - implementations of strrpos() (or last_index_of as it was called) on this page. This one is a slight modifiaction of the one below, but it should world like a *real* strrpos(), because it returns false if there is no needle in the haystack.
<?php
function my_strrpos($haystack, $needle) {
$index = strpos(strrev($haystack), strrev($needle));
if($index === false) {
return false;
}
$index = strlen($haystack) - strlen($needle) - $index;
return $index;
}
?>
I was immediatley pissed when i found the behaviour of strrpos ( shouldnt it be called charrpos ?) the way it is, so i made my own implement to search for strings.
<?
function proper_strrpos($haystack,$needle){
while($ret = strrpos($haystack,$needle))
{
if(strncmp(substr($haystack,$ret,strlen($needle)),
$needle,strlen($needle)) == 0 )
return $ret;
$haystack = substr($haystack,0,$ret -1 );
}
return $ret;
}
?>
This seems to behave like the exact equivalent to the PHP 5 offset parameter for a PHP 4 version.
<?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;
}
}
?>