當前位置: 首頁> 函數類別大全> str_ends_with

str_ends_with

檢查字符串是否以給定的子字符串結尾
名稱:str_ends_with
分類:字符串
所屬語言:php
一句話介紹:判斷一個字符串是否以指定的後綴結尾

函數名:str_ends_with()

適用版本:PHP 8.0.0 或更高版本

函數功能:判斷一個字符串是否以指定的後綴結尾

語法:bool str_ends_with ( string $haystack , string $needle )

參數:

  • $haystack:要檢查的字符串
  • $needle:要檢查的後綴

返回值:

  • 如果$haystack 以$needle 結尾,則返回true
  • 如果$haystack 不以$needle 結尾,則返回false

示例:

 $string1 = "Hello, World!"; $string2 = "Hello, PHP!"; $suffix = "World!"; // 检查$string1 是否以$suffix 结尾if (str_ends_with($string1, $suffix)) { echo "$string1 以$suffix 结尾"; } else { echo "$string1 不以$suffix 结尾"; } // 检查$string2 是否以$suffix 结尾if (str_ends_with($string2, $suffix)) { echo "$string2 以$suffix 结尾"; } else { echo "$string2 不以$suffix 结尾"; }

輸出:

 Hello, World! 以World! 结尾Hello, PHP! 不以World! 结尾

注意:在PHP 8.0.0 之前的版本中,可以使用類似的功能通過以下代碼實現:

 function str_ends_with($haystack, $needle) { $length = strlen($needle); if ($length == 0) { return true; } return substr($haystack, -$length) === $needle; }

然而,使用PHP 8.0.0 及更高版本的內置函數str_ends_with() 可以提供更簡潔和高效的方式來判斷一個字符串是否以指定的後綴結尾。

同類函數
  • 以千位分隔符方式格式化一個數字 number_format

    number_format

    以千位分隔符方式格式化一個數字
  • 查找字符串的首次出現 strstr

    strstr

    查找字符串的首次出現
  • 計算一個字符串的crc32 多項式 crc32

    crc32

    計算一個字符串的crc32多項式
  • 轉換十六進製字符串為二進製字符串 hex2bin

    hex2bin

    轉換十六進製字符串為二進製字符串
  • 刪除字符串末端的空白字符(或者其他字符) rtrim

    rtrim

    刪除字符串末端的空白字符(或者其他字符)
  • 將帶引號的可打印字符串轉換為8位字符串 quoted_printable_decode

    quoted_printable_decode

    將帶引號的可打印字符串轉換為8位字符串
  • 將格式化後的字符串寫入到流 fprintf

    fprintf

    將格式化後的字符串寫入到流
  • 設置區域設置信息 setlocale

    setlocale

    設置區域設置信息
熱門文章