Split a string by words:
In the following example, please note that we only use the string parameter the first time we call the strtok() function. After the first call, the function only requires the split parameter because it knows where it is in the current string. To split a new string, call strtok() with the string parameter again:
<?php$string = "Hello world. Beautiful day today.";$token = strtok($string, " ");while ($token != false){echo "$token<br>";$token = strtok (" ");}?>The strtok() function splits a string into smaller strings (tokens).
strtok( string,split )
parameter | describe |
---|---|
string | Required. Specifies the string to be split. |
split | Required. Specifies one or more delimiting characters. |
Return value: | Returns a string token. |
---|---|
PHP version: | 4+ |