PHP substr_count function ()
Przykłady
Policzyć "świata" pojawia się w ciągu:
<?php
echo substr_count("Hello world. The world is nice","world");
?>
echo substr_count("Hello world. The world is nice","world");
?>
Uruchomienie instancji »
substr_count () oblicza liczbę wystąpień w podciągu strun.
Uwaga: podciąg jest wielkość liter.
Uwaga: Funkcja ta nie liczy się pokrywały podciągi (patrz przykład 2).
UWAGA: Jeśli zaczniesz argument Plus Parametr długość jest większa od długości łańcucha, funkcja wygeneruje ostrzeżenie (patrz przykład 3).
gramatyka
substr_count( string,substring,start,length )
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
substring | 必需。规定要检索的字符串。 |
start | 可选。规定在字符串中何处开始搜索。 |
length | 可选。规定搜索的长度。 |
dane techniczne
Zwraca: | Zwraca liczbę wystąpień w podciągu strun. |
---|---|
Wersja PHP: | 4+ |
dziennika aktualizacji: | W PHP 5.1, dodać parametry startowe i długość. |
Więcej przykładów
Przykład 1
Wykorzystaj wszystkie parametry:
<?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 PHP"
echo substr_count($str,"is",3)."<br>"; // The string is now reduced to "s is PHP"
echo substr_count($str,"is",3,3)."<br>"; // The string is now reduced to "s i"
?>
$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 PHP"
echo substr_count($str,"is",3)."<br>"; // The string is now reduced to "s is PHP"
echo substr_count($str,"is",3,3)."<br>"; // The string is now reduced to "s i"
?>
Uruchomienie instancji »
Przykład 2
Nakładające podciąg:
<?php
$str = "abcabcab";
echo substr_count($str,"abcab"); // This function does not count overlapped substrings
?>
$str = "abcabcab";
echo substr_count($str,"abcab"); // This function does not count overlapped substrings
?>
Uruchomienie instancji »
Przykład 3
Jeśli rozpoczęcia i długości parametrów na długości łańcucha, funkcja wyświetla ostrzeżenie:
<?php
echo $str = "This is nice";
substr_count($str,"is",3,9);
?>
echo $str = "This is nice";
substr_count($str,"is",3,9);
?>
Ze względu na długość o wartości przekracza długość łańcucha (3 + 9 większe niż 12). Tak będzie wyjście ostrzeżenie.
PHP referencyjny String