Split a String into an Array of Words in PHP
Last Updated :
15 Jul, 2024
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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[0] => Hello
[1] => geek
[2] => welcome
[3] => to,
[4] => GFG!
)
Similar Reads
How to Slice an Array in PHP? In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to he
2 min read
PHP Change strings in an array to uppercase Changing strings in an array to uppercase means converting all the string elements within the array to their uppercase equivalents. This transformation modifies the array so that every string, regardless of its original case, becomes fully capitalized.Examples:Input : arr[] = ("geeks", "For", "GEEks
3 min read
How to get the position of character in a string in PHP ? In this article, we will get the position of the character in the given string in PHP. String is a set of characters. We will get the position of the character in a string by using strpos() function. Syntax: strpos(string, character, start_pos) Parameters: string (mandatory): This parameter refers t
2 min read
How to read each character of a string in PHP ? A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. Here are some approaches to read each character of a string in PHPTable of ContentUsing str_split() method - The str_
4 min read
How to insert a line break in PHP string ? In this article, we will discuss how to insert a line break in a PHP string. We will get it by using the nl2br() function. This function is used to give a new line break wherever '\n' is placed.Syntax:nl2br("string \n");where the string is the input string.Example 1: PHP Program to insert a line bre
2 min read