PHP 程式語言的 substr_count() 函數也是重要的內建函數之一,它對於計算特定給定字串中出現了多少個子字串非常有幫助。此函數將為給定索引範圍內的某些給定子字串提供搜尋選項。 substr_count() 區分大小寫,這表示在字串“acbcab”中不存在“abc”子字串值。如果為搜尋指定的 (start+length) 值超出了傳遞的字串的大小,則會向使用者傳回警告訊息。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
文法如下:
Substr_count($string1, $substring1, $start1, $length1)
substr_count()函數參數說明:
1。 $string1: PHP 程式語言的 substr_count() 的 string1 參數有助於傳遞參數,該參數實際上是計算子字串出現次數的參數。 string1 參數是 substr_count() 函數的強制參數之一。需要它才能使 substr_count() 正常運作。
2。 $substring1: PHP 程式語言的 substr_count() 的 substring1 參數有助於傳遞子字串,其中字串搜尋和出現次數將被統計並傳回。必須提供此 substring1 參數,因為它是強制參數。
3。 $start1: PHP 程式語言的 substr_count() 的 start1 參數有助於將某些值傳遞給該參數,但它不是 PHP substr_count() 函數的強制參數。它是一個可選參數。參數傳遞值後,搜尋將完成,並從起始位置開始,而不是整個字串搜尋不同出現的子字串。
4。 $length1: PHP 程式語言的 substr_count() 的 length1 參數將有助於限制實際從 start 開始到 start+length 位置的搜尋操作。如果 start+length 值將增加字串長度,則透過提供/產生警告訊息。此參數根本不是強制參數。這是一個可選參數。
substr_count() 函數透過傳回不同類型的值來運作,這些值在不同類型的情況下如下所示。
以下是 substr_count() 函數的範例:
這是僅使用兩個強制參數實作 substr_count() 的範例。它們是原始字串和子字串。將在 substr_count() 內部的原始來源字串中搜尋子字串,以了解子字串出現了多少次。
代碼:
<?php $str1 = "pavan sake pavan"; echo "<hr>"; echo "The Original Source String is :: $str1"; echo "<br>"; $substring1 = "pavan"; echo "The Sub String which is to be searched in the original source string :: $substring1"; echo "<br>"; echo "The number of occurrences of the substring ($substring1) is :: "; echo substr_count($str1, $substring1); echo "<hr>"; ?>
輸出:
這是傳入start參數時實作substr_count()函數的範例。
代碼:
<?php echo "This is the example of implementing start1 variable/parameter inside of substring_count() function"; echo "<br>"; $str1 = "pavankumar sake pavan"; echo "<hr>"; echo "The Original Source String is :: $str1"; echo "<br>"; $substring1 = "pavan"; echo "The Sub String which is to be searched in the original source string :: $substring1"; echo "<br>"; echo "The number of occurrences of the substring ($substring1) after the string length 6 is :: "; echo substr_count($str1, $substring1, 6); echo "<hr>"; ?>
輸出:
這是在同時傳遞 start1 和 length1 參數時實作 substr_count() 的範例。這將繞過搜尋 2 次。
代碼:
<?php echo "This is the example of implementing length1 and start1 variables/parametersinside of substring_count() function :: "; echo "<br>"; $str1 = "pavankumar sake pavan sake sakesakesakesake"; echo "<hr>"; echo "The Original Source String of str1 variable is :: $str1"; echo "<br>"; $substring1 = "pavan"; echo "The Sub String substring1 variable which is to be searched in the original source string :: $substring1"; echo "<br>"; echo "The number of occurrences of the substring1 ($substring1) after the start value 6 and length value 2 is :: "; echo substr_count($str1, $substring1, 6, 2); echo "<hr>"; ?>
輸出:
This is the example of implementing the substr_count() function when the string length exceeds the start1+length1 parameters value. This will produce a warning type of message. Check out the output so that you will understand the substr_count() function.
Code:
<?php echo "This is the example of implementing length1 and start1 variables/parameters inside of substring_count() function for warning message if length exceeds the string length :: "; echo "<br>"; $str1 = "pavankumar sake pavan sake sakesakesakesake"; echo "<hr>"; echo "The Original Source String of str1 variable is :: $str1"; echo "<br>"; $substring1 = "pavan"; echo "The Sub String substring1 variable which is to be searched in the original source string :: $substring1"; echo "<br>"; echo "<hr>"; echo "The number of occurrences of the substring1 ($substring1) after the start value 6 and length value 2 is :: "; echo "<hr>"; echo substr_count($str1, $substring1, 6, 121); echo "<hr>"; ?>
Output:
I hope you learned what is the definition of the PHP substr_count() function along with substr_count()’s syntax and parameter, how the PHP substr_count() function works in the PHP Programming language along with various examples to understand the substr_count() concept better and so easily.
This is a guide to the PHP substr_count(). Here we discuss the Introduction and its parameters of PHP substr_count() Function along with examples and Code Implementation. You can also go through our other suggested articles to learn more-
以上是PHP substr_count()的詳細內容。更多資訊請關注PHP中文網其他相關文章!