How to split a string into an array in PHP?
Answer: Using explode()
function
We can split a string into an array using PHP explode()
function. The PHP explode() function returns an array of strings by splitting a string using a separator.
Syntax
The syntax of explode()
function is given below:
explode(separator,string,limit)
The explode()
function takes three parameters, which are given below:
- seperator - This parameter specifies where to break the string.
- string - it is the input string
- limit - This parameter specifies the number of array elements to be returned. This is optional.
Example: Splitting string into an array using explode() function
In the given example, we have converted the given string into the array of strings using the explode()
function.
<!DOCTYPE html>
<html>
<head>
<title>Split a string into an array</title>
</head>
<body>
<?php
$string = 'Welcome to Studytonight';
$newStr = explode(' ', $string);
print_r($newStr);
?>
</body>
</html>
Array ( [0] => Welcome [1] => to [2] => Studytonight )
Using preg_split()
function
We can also split the given string into an array using the preg_split()
function. The preg_split()
function converts the given string into the array of strings. We can also specify the length of the array elements using this function.
The preg_split()
function is quite similar to the explode() function, but they are slightly different. We have to use the regular expression while specifying the separator in the preg_split()
function, while in explode function, we do not use regular expression.
Example: Splitting string into array using preg_split()
function
In the given example, we have converted the give string into the array of string using the preg_split()
function.
<!DOCTYPE html>
<html>
<head>
<title>Split a string into an array</title>
</head>
<body>
<?php
$string = 'Hello! Welcome to Studytonight';
$newStr = preg_split ("/\ /", $string);
print_r($newStr);
?>
</body>
</html>
Array ( [0] => Hello! [1] => Welcome [2] => to [3] => Studytonight )
Using str_split()
function
The str_split()
function is also used to split the given string into the string of the array. By default, this function split the string into an array of characters but we can also specify the length of the elements within the array.
Example: Splitting string into array using str_split()
function
In the given example, we have split the string into the array using the str_split()
function.
<!DOCTYPE html>
<html>
<head>
<title>Split a string into an array</title>
</head>
<body>
<?php
$string = 'Hello! Welcome to Studytonight';
$newStr = str_split($string, 5);
print_r($newStr);
?>
</body>
</html>
Array ( [0] => Hello [1] => ! Wel [2] => come [3] => to St [4] => udyto [5] => night )
Conclusion
In this lesson, we have learned how to split a string into an array. Here, we have discussed three methods to convert the given string into an array of strings. At first, we have converted the given string into an array of strings using the explode()
function. Then we did the same using the preg_split()
function. In the preg_split() function, we have to use the regular expression for specifying the separator. Then we have used the str_split()
function. By default, this function converts the given string into the array of characters, but we can also specify the length of the array elements in this function.