當前位置: 首頁> 函數類別大全> 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() 可以提供更簡潔和高效的方式來判斷一個字符串是否以指定的後綴結尾。

同類函數
  • 將字符串解析成多個變量 parse_str

    parse_str

    將字符串解析成多個變量
  • 返回格式化的字符串 vsprintf

    vsprintf

    返回格式化的字符串
  • 以千位分隔符方式格式化一個數字 number_format

    number_format

    以千位分隔符方式格式化一個數字
  • 從字符串的開頭和結尾去除空格(或其他字符) trim

    trim

    從字符串的開頭和結尾去除空格(或其他字符
  • 在字符串中查找一組字符的任何一個字符-返回一個以找到的字符開始的子字符串 strpbrk

    strpbrk

    在字符串中查找一組字符的任何一個字符-返
  • 將8位字符串轉換為帶引號的可打印字符串 quoted_printable_encode

    quoted_printable_encode

    將8位字符串轉換為帶引號的可打印字符串
  • 解碼一個uuencode 編碼的字符串 convert_uudecode

    convert_uudecode

    解碼一個uuencode編碼的字符串
  • 將格式化的字符串寫入流 vfprintf

    vfprintf

    將格式化的字符串寫入流
熱門文章