str_contains() 是 PHP 8 版本中引入的預定義函數。 str_contains() 函數在表達式中搜索給定字符串中的子字符串。如果提到的子字符串將出現在字符串中,則它將返回 True,否則將返回 False。 str_contains() 函數與 strpos() 函數非常相似。
特性:
- 它總是返回一個布爾值。
- 如果檢查子字符串為空,它將返回 TRUE。
- 它區分大小寫。
- 此函數是二進製安全的。
- 它僅在 PHP 8 或更高版本上受支持。
用法:
(str_contains('String', 'Substring')) ;
子字符串是需要搜索的字符串,而字符串是要搜索子字符串的部分。
注意:str_contains() 僅在 PHP 8 或更高版本中支持。
例:在下麵的例子中,我們在 $sentance 中存儲了一個句子,在 $word 中存儲了一個單詞。在本例中,我們嘗試使用 str_contains() 函數檢查句子中是否存在該單詞。如果單詞將出現在句子中,‘is’ 將被分配給 $result 否則它的值將是“不是”。我們將通過示例來理解這一點。
PHP
<?php
$sentance = 'GFG is Awesome';
$word = 'GFG';
$result = str_contains($sentance, $word) ? 'is' :'is not';
echo "The word {$word} {$result} present in the sentance \"{$sentance}\" ";
?>
從上麵,我們已經看到如何使用 str_contains() 函數查找給定字符串中的子字符串,返回值將是布爾值。
輸出:
The word GFG is present in the sentance "GFG is Awesome"
相關用法
- underscore.js _.strContains()用法及代碼示例
- Lodash _.strContains()用法及代碼示例
- 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()用法及代碼示例
注:本文由純淨天空篩選整理自talktoosaurabh大神的英文原創作品 PHP str_contains() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。