Open In App

Split a String into an Array of Words in PHP

Last Updated : 15 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In PHP, splitting a string into an array involves breaking a string into smaller parts based on a delimiter or a pattern and storing those parts as elements of an array.

PHP provides several methods to achieve this, making it easy to convert a string into an array of words:

Using explode() method

Splits a string into an array of substrings based on a specified delimiter.

Syntax:

 array explode (string $separator , string $string [, int $limit = PHP_INT_MAX]).

Example: This example shows the use of explode() method for splitting the string into an array.

PHP
<?php
// Using explode() function
$string = "Hello world, welcome to PHP programming!";
$wordsArray = explode(" ", $string);
print_r($wordsArray);
?>

Output
Array
(
    [0] => Hello
    [1] => world,
    [2] => welcome
    [3] => to
    [4] => PHP
    [5] => programming!
)

Using str_word_count() method

Splits a string into an array of characters, with each character becoming an element of the array.

Syntax:

array str_word_count ( string $string [, int $format = 0 [, string $charlist ]] ).

Example: This example shows the use of str_word_count() method for splitting the string into an array.

PHP
<?php
// Using str_split() function
$string = "Hello world, welcome to PHP programming!";
$wordsArray = str_word_count($string, 1);
print_r($wordsArray);
?>

Output
Array
(
    [0] => Hello
    [1] => world
    [2] => welcome
    [3] => to
    [4] => PHP
    [5] => programming
)

Using preg_split() method

Splits a string into an array using a regular expression pattern as the delimiter.

Syntax:

array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )

Example: This example shows the use of preg_split()method for splitting the string into an array.

PHP
<?php
// Using preg_split() function
$string = "Hello world, welcome to PHP programming!";
$wordsArray = preg_split("/[\s,]+/", $string);
print_r($wordsArray);
?>

Output
Array
(
    [0] => Hello
    [1] => world
    [2] => welcome
    [3] => to
    [4] => PHP
    [5] => programming!
)

Using for loop

In this approach we splits a string into words by iterating through each character, appending non-whitespace characters to word variable and adding completed words to words when a whitespace character is encountered.

Example: This example shows the use of for loop for splitting the string into an array.

PHP
<?php
$string = "Hello geek  welcome to, GFG!";
$words = [];
$word = '';

for ($i = 0; $i < strlen($string); $i++) {
    $char = $string[$i];

    // Check for whitespace character
    if ($char !== ' ') {
        $word .= $char; // Append character to current word
    } else {
        if (!empty($word)) {
            $words[] = $word; // Add completed word to array
            $word = ''; // Reset current word
        }
    }
}
if (!empty($word)) {
    $words[] = $word; // Add last word if any
}


print_r($words); 
?>

Output
Array
(
    [0] => Hello
    [1] => geek
    [2] => welcome
    [3] => to,
    [4] => GFG!
)

Next Article

Similar Reads