PHP substr_count() Funksie
Voorbeeld
Tel die aantal kere wat "wêreld" in die string voorkom:
<?php
echo substr_count("Hello world. The world is nice","world");
?>
Die substr_count() funksie tel die aantal kere wat 'n substring in 'n string voorkom.
Let wel: Die substring is hooflettersensitief.
Let wel: Hierdie funksie tel nie oorvleuelde substringe nie (sien voorbeeld 2).
Let wel: Hierdie funksie genereer 'n waarskuwing as die begin parameter plus die lengte parameter groter is as die string lengte (sien voorbeeld 3).
Sintaksis
substr_count(string,substring,start,length)
Parameterwaardes
Parameter | Description |
---|---|
string | Required. Specifies the string to check |
substring | Required. Specifies the string to search for |
start | Optional. Specifies where in string to start searching. If negative, it starts counting from the end of the string |
length | Optional. Specifies the length of the search |
Tegniese besonderhede
Terugkeerwaarde: | Wys die aantal kere wat die substring in die string voorkom |
---|---|
PHP weergawe: | 4+ |
Veranderinglog: | PHP 7.1 - Die lengte parameters kan 0 of 'n negatiewe getal wees. PHP 7.1 - Die beginparameters kan 'n negatiewe getal wees. PHP 5.1 - Die begin- en lengteparameters is bygevoeg. |
Meer voorbeelde
Voorbeeld
Gebruik alle parameters:
<?php
$str = "This is nice";
echo strlen($str)."<br>"; // Using strlen() to return the string length
echo substr_count($str,"is")."<br>"; // The number of times "is" occurs in the string
echo substr_count($str,"is",2)."<br>"; // The string is now reduced to "is is nice"
echo substr_count($str,"is",3)."<br>"; // The string is now reduced to "s is nice"
echo substr_count($str,"is",3,3)."<br>"; // The string is now reduced to "s i"
?>
Voorbeeld
Oorvleuelde substringe:
<?php
$str = "abcabcab";
echo substr_count($str,"abcab"); // This function does not count overlapped substrings
?>
Voorbeeld
As die begin- en lengteparameters die stringlengte oorskry, sal hierdie funksie 'n waarskuwing uitstuur:
<?php
echo $str = "This is nice";
substr_count($str,"is",3,9);
?>
Dit sal 'n waarskuwing gee omdat die lengtewaarde die stringlengte oorskry (3+9 is groter as 12)
❮ PHP-stringverwysing