Split a string into words in PHP
Question:
How to split a string into words in PHP? Answer:
$myText = 'This is a text with multiple words';
$words = explode(' ', $myText);
foreach ($words as $word) {
echo $word . '<br/>';
}
Description:
In PHP to split or divide a string into words, you can use the explode
method, that splits a string by a string.
Syntax:
explode(string $separator, string $string, int $limit = PHP_INT_MAX): array
When found, $separator
is removed from the string, and the substrings are returned in an array.
Reference:
The explode() reference
Share "How to split a string into words in PHP?"
Related snippets:
- Remove whitespace from the beginning and end of a string in PHP
- Locate a substring within a string in PHP
- Process CSV line in PHP
- Extract a substring from a string in PHP
- Split a string into words in PHP
- Replace spaces with dashes in PHP
- Replace only the first occurrence of a string in PHP
- Replace last occurrence of a string in a string in PHP
- Remove all attributes from HTML tags in PHP
- Remove HTML tags from a string except p, b, i and br in PHP
- Remove HTML tags from a string in PHP
Tags:
split, divide, string, words, space, explode, break, php Technical term:
Split a string into words in PHP