str_ends_with() 函數是 PHP 8 中的預定義函數,用於對給定字符串執行區分大小寫的搜索。 str_ends_with() 通常檢查字符串是否以子字符串結尾。如果字符串以子字符串結尾,則 str_ends_with() 將返回 TRUE,否則將返回 FALSE。它與 str_starts_with() 非常相似,str-starts_with() 和 str_ends_with() 之間的唯一主要區別是 str-starts_with() 在字符串開頭搜索子字符串,而 str_ends_with() 在字符串末尾搜索子字符串。
用法:
str_starts_with(string $string, string $substring)
參數:
- $string:$string 是指需要搜索結束字符串的字符串。
- $substring:$substring 是 $string 中需要搜索的字符串。
主要特征:
- 它本質上區分大小寫。
- 它總是返回一個布爾值。
- 它可用於檢查 Sting 和 Character 的結尾。
- 如果子字符串為空或 NULL,它總是返回 TRUE,因為每個字符串都以 NULL 結尾。
- 小於 8 的 PHP 版本不支持它。
範例1:在這個例子中,已經創建了三個變量。 $string 存儲字符串值, $endsWith 存儲將在 $string 末尾搜索的值, $result 存儲結果值。在程序中,如果字符串 $endsWith 位於 $string 的末尾,則函數 str_ends_with() 將返回 TRUE,否則它將向三元運算符返回 FALSE,並且將相應地分配變量 $result 的值。
PHP
<?php
$string = 'GFG is awesome';
$endsWith = 'some';
$result = str_ends_with($string, $endsWith) ? 'is' :'is not';
echo "The string \"$string\" $result ending with $endsWith";
?>
輸出:
The string "GFG is awesome" is ending with some
範例2:在這個例子中,我們創建了兩個變量 $string 來存儲字符串和 $endsWith 來存儲 $string 末尾需要檢查的結束值。在這裏, $endsWith 的值將為空,我們將檢查字符串是否以空字符串結尾。在這種情況下,if-condition 的輸出將始終為 TRUE,因為所有字符串都以“\0”結尾,並且輸出將相應地顯示。
PHP
<?php
$string = 'Checking for Blanks in the string';
$endsWith = '';
if(str_ends_with($string, $endsWith)) {
echo 'Given String ends with an empty string';
}
else {
echo 'Given String does not ends with an empty string';
}
?>
輸出:
Given String ends with an empty string
參考: https://www.php.net/manual/en/function.str-ends-with.php
相關用法
- PHP imagecreatetruecolor()用法及代碼示例
- PHP fpassthru( )用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- PHP Imagick floodFillPaintImage()用法及代碼示例
- PHP geoip_continent_code_by_name()用法及代碼示例
- PHP GmagickPixel setcolor()用法及代碼示例
- PHP opendir()用法及代碼示例
- PHP stream_get_transports()用法及代碼示例
- PHP Ds\Deque pop()用法及代碼示例
- PHP is_numeric()用法及代碼示例
- PHP Imagick adaptiveSharpenImage()用法及代碼示例
- PHP XMLWriter endDtdEntity()用法及代碼示例
- PHP isset()用法及代碼示例
- PHP ctype_print()用法及代碼示例
- PHP Imagick flopImage()用法及代碼示例
- PHP imagecreatefrompng()用法及代碼示例
- PHP imagechar()用法及代碼示例
注:本文由純淨天空篩選整理自talktoosaurabh大神的英文原創作品 PHP str_ends_with() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。