©
本文档使用
php.cn手册 发布
(PHP 4, PHP 5)
strrchr — 查找指定字符在字符串中的最后一次出现
$haystack
, mixed $needle
)
该函数返回 haystack
字符串中的一部分,这部分以 needle
的最后出现位置开始,直到 haystack
末尾。
haystack
在该字符串中查找。
needle
如果 needle
包含了不止一个字符,那么仅使用第一个字符。该行为不同于 strstr() 。
如果 needle
不是一个字符串,那么将被转化为整型并被视为字符顺序值。
该函数返回字符串的一部分。如果 needle
未被找到,返回 FALSE
。
版本 | 说明 |
---|---|
4.3.0 | 该函数是二进制安全的。 |
Example #1 strrchr() 范例
<?php
// 获取 $PATH 中不含磁盘符号的目录
$dir = substr ( strrchr ( $PATH , ":" ), 1 );
// 获取最后一行内容
$text = "Line 1\nLine 2\nLine 3" ;
$last = substr ( strrchr ( $text , 10 ), 1 );
?>
Note: 此函数可安全用于二进制对象。
[#1] jphansen at uga dot edu [2012-08-15 16:16:15]
To extract your portion of a string without the actual character you searched for, you can use:
<?php
$path = '/www/public_html/index.html';
$filename = substr(strrchr($path, "/"), 1);
echo $filename; // "index.html"
?>
[#2] matthewkastor at live dot com [2011-03-30 13:07:24]
<?php
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);
}
?>
[#3] readless at gmx dot net [2006-06-06 19:55:42]
to: repley at freemail dot it
the code works very well, but as i was trying to cut script names (e.g.: $_SERVER["SCRIPT_NAME"] => /index.php, cut the string at "/" and return "index.php") it returned nothing (false). i've modified your code and now it works also if the needle is the first char.
- regards from germany
<?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)){
//Everything before last $needle in $haystack.
$left = substr($haystack, 0, strrpos($haystack, $needle) + $l_inclusive);
//Switch value of $r_inclusive from 0 to 1 and viceversa.
$r_inclusive = ($r_inclusive == 0) ? 1 : 0;
//Everything after last $needle in $haystack.
$right = substr(strrchr($haystack, $needle), $r_inclusive);
//Return $left and $right into an array.
return array($left, $right);
} else {
if(strrchr($haystack, $needle)) return array('', substr(strrchr($haystack, $needle), $r_inclusive));
else return false;
}
}
?>
[#4] sekati at gmail dot com [2006-04-08 17:43:23]
just a small addition to carlos dot lage at gmail dot com note which makes it a bit more useful and flexible:
<?php
// return everything up to last instance of needle
// use $trail to include needle chars including and past last needle
function reverse_strrchr($haystack, $needle, $trail) {
return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
}
// usage:
$ns = (reverse_strrchr($_SERVER["SCRIPT_URI"], "/", 0));
$ns2 = (reverse_strrchr($_SERVER["SCRIPT_URI"], "/", 1));
echo($ns . "<br>" . $ns2);
?>
[#5] freakinunreal at hotmail dot com [2005-12-24 22:54:06]
to marcokonopacki at hotmail dot com.
I had to make a slight change in your function for it to return the complete needle inclusive.
// Reverse search of strrchr.
function strrrchr($haystack,$needle)
{
// Returns everything before $needle (inclusive).
//return substr($haystack,0,strpos($haystack,$needle)+1);
// becomes
return substr($haystack,0,strpos($haystack,$needle)+strlen($needle));
}
Note: the +1 becomes +strlen($needle)
Otherwise it only returns the first character in needle backwards.
[#6] Primo Anderson Do S?tio [2005-07-29 10:24:18]
$filename = 'strrchr_test.php';
print strrchr( $filename, '.' );
Result:
.php
$other_filename = 'strrchr_test.asp.php';
print strrchr( $other_filename, '.' );
Result:
.php
[#7] carlos dot lage at gmail dot com [2005-04-03 11:45:03]
I used dchris1 at bigpond dot net dot au 's reverse strrchr and reduced it to one line of code and fixed it's functionality - the real strrchr() returns FALSE if the needle is not found, not the haystack :)
<?php
// reverse strrchr()
function reverse_strrchr($haystack, $needle)
{
return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
}
?>
[#8] dchris1 at bigpond dot net dot au [2004-02-16 19:16:20]
The function provided by marcokonopacki at hotmail dot com isn't really a reverse-version of strrchr(), rather a reverse version of strchr(). It returns everything from the start of $haystack up to the FIRST instance of the $needle. This is basically a reverse of the behavior which you expect from strchr(). A reverse version of strrchr() would return everything in $haystack up to the LAST instance of $needle, eg:
<?php
// reverse strrchr() - PHP v4.0b3 and above
function reverse_strrchr($haystack, $needle)
{
$pos = strrpos($haystack, $needle);
if($pos === false) {
return $haystack;
}
return substr($haystack, 0, $pos + 1);
}
?>
Note that this function will need to be modified slightly to work with pre 4.0b3 versions of PHP due to the return type of strrpos() ('0' is not necessarily 'false'). Check the documentation on strrpos() for more info.
A function like this can be useful for extracting the path to a script, for example:
<?php
$string = "/path/to/the/file/filename.php";
echo reverse_strrchr($string, '/'); // will echo "/path/to/the/file/"
?>
[#9] andfarm at thibs dot menloschool dot org [2003-05-23 16:24:48]
strrchr is also very useful for finding the extension of a file. For example:
$ext = strrchr($filename, ".");
and $ext will contain the extension of the file, including a ".", if the file has an extension, and FALSE if the file has no extension. If the file has multiple extensions, such as "evilfile.jpg.vbs", then this construction will just return the last extension.
[#10] marcokonopacki at hotmail dot com [2003-04-14 12:43:48]
<?php
// Reverse search of strrchr.
function strrrchr($haystack,$needle)
{
// Returns everything before $needle (inclusive).
return substr($haystack,0,strpos($haystack,$needle)+1);
}
$string = "FIELD NUMBER(9) NOT NULL";
echo strrrchr($string,")"); // Will print FIELD (9)
?>