這是 PHP 8 中的一個預定義函數,用於對給定字符串執行區分大小寫的搜索。 str_starts_with() 通常會檢查字符串是否以子字符串開頭。如果字符串以子字符串開頭,則 str_starts_with() 將返回 TRUE,否則將返回 FALSE。
用法:
str_starts_with($string, $substring)
參數:
- $string:該參數是指需要檢查起始字符串的字符串。
- $substring:該參數指的是需要檢查的字符串。
返回類型:如果字符串以子字符串開頭,則 str_starts_with() 將返回 TRUE,否則將返回 FALSE。
主要特征:
- str_starts_with()本質上區分大小寫。
- str_starts_with()總是返回一個布爾值。
- str_starts_with()可用於檢查字符和字符串的開頭。
- str_starts_with()小於 8 的 PHP 版本不支持。
範例1:在下麵的程序中,我們創建了三個變量 $name 來存儲類型字符串的名稱, $beginsWith 存儲需要用 $name 檢查的子字符串,以及 $result 存儲基於str_starts_with()。如果字符串 $name 將以子字符串 $beginsWith 開頭,則 str_starts_with() 將返回 TRUE,否則將返回 FALSE,並相應地分配 $result 的值。
PHP
<?php
$name = 'Saurabh Singh';
$beginsWith = 'S';
$result = str_starts_with($name, $beginsWith) ? 'is' :'is not';
echo "The string \"$name\" $result starting with $beginsWith";
?>
輸出:
The string "Saurabh Singh" is starting with S
範例2:在第一個示例中,我們使用句子的開頭字符進行搜索。在這個例子中,我們取了一個完整的單詞作為句子的開頭,它也會在 if 條件中返回 TRUE,然後相應地執行條件部分。
PHP
<?php
$sentance = 'The Big Brown Fox';
$beginsWith = 'The';
if(str_starts_with($sentance , $beginsWith) )
{
echo "The string \"$sentance\" begins with \"$beginsWith\" ";
}
else
{
echo "The string \"$sentance\" does not begins with \"$beginsWith\" ";
}
?>
輸出:
The string "The Big Brown Fox" begins with "The"
參考:https://www.php.net/manual/en/function.str-starts-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 cal_to_jd()用法及代碼示例
- PHP stream_get_transports()用法及代碼示例
- PHP Ds\Deque pop()用法及代碼示例
- PHP SimpleXMLElement children()用法及代碼示例
- PHP is_numeric()用法及代碼示例
- PHP Imagick adaptiveSharpenImage()用法及代碼示例
- PHP XMLWriter endDtdEntity()用法及代碼示例
- PHP isset()用法及代碼示例
- PHP ctype_print()用法及代碼示例
- PHP Imagick flopImage()用法及代碼示例
注:本文由純淨天空篩選整理自talktoosaurabh大神的英文原創作品 PHP str_starts_with() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。