qual è la sintassi della funzione SUBSTR_COUNT() in php?
substr_count(string,substring,start,length)
Parametro | DESCRIZIONE |
---|---|
stringa | Necessario. Specifica la stringa da controllare |
sottostringa | Necessario. Specifica la stringa da cercare |
inizia a | Opzionale. Specifica dove nella stringa iniziare la ricerca. Se negativo, inizia il conteggio dalla fine della stringa |
lunghezza | Opzionale. Specifica la lunghezza della ricerca |
esempi della funzione SUBSTR_COUNT()
Esempio 1. In questo esempio, contiamo il numero di volte in cui "mondo" si verifica nella stringa.
<?php
echo substr_count("Hello world. The world is nice","world");
?>
Esempio 2. In questo esempio, utilizziamo tutti i parametri.
<?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"
?>
Esempio 3. In questo esempio, abbiamo sovrapposto le sottostringhe.
<?php
$str = "abcabcab";
echo substr_count($str,"abcab"); // This function does not count overlapped substrings
?>
Esempio 3. In questo esempio, se i parametri di inizio e lunghezza superano la lunghezza della stringa, questa funzione genererà un avviso.
<?php
echo $str = "This is nice";
substr_count($str,"is",3,9);
?>