strrpos()
是 PHP 中的一個字符串函數(shù),它用于查找一個字符串中最后一個指定子字符串出現(xiàn)的位置
以下是 strrpos()
函數(shù)的一些常見用法:
$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()
函數(shù)在處理字符串時非常有用,尤其是在需要查找子字符串在字符串中最后一次出現(xiàn)位置的情況下。