PHP

php strrpos怎样使用

小樊
81
2024-11-19 18:59:53
栏目: 编程语言

strrpos() 是 PHP 中的一个字符串函数,用于查找一个字符串在另一个字符串中最后一次出现的位置。函数原型如下:

strrpos(string $haystack, string $needle, int $offset = 0): int

参数说明:

返回值:

示例:

<?php
$haystack = "Hello, I am a PHP developer.";
$needle = "PHP";

// 从字符串的开头开始搜索
$position = strrpos($haystack, $needle);
if ($position !== false) {
    echo "The last occurrence of '{$needle}' is at position: {$position}.";
} else {
    echo "'{$needle}' not found in the string.";
}

// 从字符串的第 10 个字符开始搜索
$position = strrpos($haystack, $needle, 10);
if ($position !== false) {
    echo "The last occurrence of '{$needle}' starting from position 10 is at position: {$position}.";
} else {
    echo "'{$needle}' not found in the string starting from position 10.";
}
?>

输出:

The last occurrence of 'PHP' is at position: 27.
The last occurrence of 'PHP' starting from position 10 is at position: 38.

0
看了该问题的人还看了