PHP substr_count() İşlev
Örnek
Dizede "dünya"nın kaç kez geçtiğini sayın:
<?php
echo substr_count("Hello world. The world is nice","world");
?>
substr_count() işlevi, bir dizede bir alt dizenin kaç kez oluştuğunu sayar.
Not: Alt dize büyük/küçük harf duyarlıdır.
Not: Bu işlev, çakışan alt dizileri saymaz (bkz. örnek 2).
Not: Bu işlev, başlatma parametresi artı uzunluk parametresi dize uzunluğundan büyükse bir uyarı oluşturur (bkz. örnek 3).
Sözdizimi
substr_count(string,substring,start,length)
Parametre Değerleri
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 |
Teknik detaylar
Geri dönüş değeri: | Alt dizenin dizede kaç kez oluştuğunu döndürür |
---|---|
PHP Sürümü: | 4+ |
Değişiklik günlüğü: | PHP 7.1 - Uzunluk parametreleri 0 veya negatif bir sayı olabilir. PHP 7.1 - Başlangıç parametreleri negatif bir sayı olabilir. PHP 5.1 - Başlangıç ve uzunluk parametreleri eklendi. |
Daha fazla örnek
Örnek
Tüm parametreleri kullanarak:
<?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"
?>
Örnek
Çakışan alt dizeler:
<?php
$str = "abcabcab";
echo substr_count($str,"abcab"); // This function does not count overlapped substrings
?>
Örnek
Başlangıç ve uzunluk parametreleri dize uzunluğunu aşarsa, bu işlev bir uyarı verir:
<?php
echo $str = "This is nice";
substr_count($str,"is",3,9);
?>
Bu, uzunluk değeri dize uzunluğunu (3+9, 12'den büyüktür) aştığı için bir uyarı verir.
❮ PHP Dize Referansı