- Instant help with your Php coding problems

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.

Share "How to split a string into words in PHP?"
Interesting things
ChatGPT in a nutshell