阅读量:4
strrpos() 是 PHP 中的一个字符串函数,它用于查找一个字符串中最后一个指定子字符串出现的位置
以下是 strrpos() 函数的一些常见用法:
- 在字符串中查找子字符串的最后一次出现位置:
$haystack = 'Hello, welcome to the world of PHP!';
$needle = 'PHP';
$position = strrpos($haystack, $needle);
echo "The last occurrence of '{$needle}' is at position: {$position}\n"; // 输出 "The last occurrence of 'PHP' is at position: 27"
- 检查子字符串是否存在于字符串中:
$haystack = 'Hello, welcome to the world of PHP!';
$needle = 'Python';
$position = strrpos($haystack, $needle);
if ($position !== false) {
echo "The string '{$needle}' is found at position: {$position}\n";
} else {
echo "The string '{$needle}' is not found in the given string.\n";
} // 输出 "The string 'Python' is not found in the given string."
- 从字符串末尾开始查找子字符串:
$haystack = 'Hello, welcome to the world of PHP!';
$needle = 'PHP';
$position = strrpos($haystack, $needle);
if ($position !== false) {
echo "The last occurrence of '{$needle}' is at position: {$position}\n"; // 输出 "The last occurrence of 'PHP' is at position: 27"
} else {
echo "The string '{$needle}' is not found in the given string.\n";
}
总之,strrpos() 函数在处理字符串时非常有用,尤其是在需要查找子字符串在字符串中最后一次出现位置的情况下。
以上就是关于“php strrpos能干嘛用”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm