Funzione strtok
La funzione strtok suddivide sequenzialmente una stringa in token (parti) utilizzando i delimitatori specificati. La prima chiamata della funzione accetta la stringa e i delimitatori, le chiamate successive lavorano sulla stessa stringa fino a quando non restituiscono tutti i token. Quando i token sono terminati, la funzione restituisce false.
Sintassi
strtok(string, token);
Esempio
Suddividiamo una stringa utilizzando gli spazi come delimitatori:
<?php
$string = "Hello world! How are you?";
$token = strtok($string, " ");
while ($token !== false) {
echo $token . "\n";
$token = strtok(" ");
}
?>
Risultato dell'esecuzione del codice:
Hello
world!
How
are
you?
Esempio
Suddividiamo una stringa utilizzando più delimitatori:
<?php
$string = "one,two-three.four";
$token = strtok($string, ",-.");
while ($token !== false) {
echo $token . "\n";
$token = strtok(",-.");
}
?>
Risultato dell'esecuzione del codice:
one
two
three
four