溫馨提示×

php strrpos能干嘛用

PHP
小樊
81
2024-11-19 19:02:53
欄目: 編程語言

strrpos() 是 PHP 中的一個字符串函數(shù),它用于查找一個字符串中最后一個指定子字符串出現(xiàn)的位置

以下是 strrpos() 函數(shù)的一些常見用法:

  1. 在字符串中查找子字符串的最后一次出現(xiàn)位置:
$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"
  1. 檢查子字符串是否存在于字符串中:
$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."
  1. 從字符串末尾開始查找子字符串:
$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)位置的情況下。

0