PHP Convert String to Array PHP
Php 24-Jul-2021

PHP Convert String to Array PHP

Convert strings or comma separated strings to arrays in PHP using explode() function. This tutorial demonstrates, how to convert a string, comma-separated strings into an array in PHP with example and demonstration.

Convert Strings to Arrays in PHP

Before taking an example of converting strings into arrays in PHP. First, we will learn what is explode function and how it works in PHP.

PHP Explode() Function

Basically the PHP explode() function is used to split or break strings to substrings by a given delimiter in PHP. This returns an array of broken substrings that you can assign to arrays variable in PHP.

In other words, the PHP explode() function is used to convert a string or a comma-separated string into an array.

Syntax Of Explode Function

The basic syntax of explode() function is:

explode(separator,string,limit)

Parameters of explode function In PHP

Parameter Description
separator Required. Specifies where to break the string
string Required. The string to split
limit Optional. Specifies the number of array elements to return.Possible values:Greater than 0 – Returns an array with a maximum of limit element(s)Less than 0 – Returns an array except for the last -limit elements()0 – Returns an array with one element

Let’s take different types of examples of convert string into an array

First Example for convert string into an array.

In the following example, we have declared a string variable and assigned it a into a $string variable:

<?php
$string = "I love PHP";
print_r (explode(" ",$string));
?>
 
///output
 
Array ( [0] => I [1] => love [2] => PHP )

First Example Source Code

<!DOCTYPE html>
<html>
<title>Convert Strings to Arrays in PHP Example</title>
<head></head>
<body>
 
<h4>Break a string into an array In PHP</h4>
 
<?php
$string= "I love PHP";
print_r (explode(" ",$string));
?>
 
</body>
</html>

The second example for a string converted to a string.

In the following example, we have declared a new string variable and given it a number in this format:

<?php
 $nStr = "001-234-567678";
 print_r (explode("-",$nStr));
?>
 
//output
 
Array ( [0] => 001 [1] => 234 [2] => 567678 )

After that, the explode method is used to split a string using a hyphen (dash) delimiter. The returned array is assigned to the array.

<!DOCTYPE html>
<html>
<title>Convert Number Strings to Arrays in PHP Example</title>
<head></head>
<body>
 
<h4>Break a number string into an array In PHP</h4>
 
<?php
 $nStr = "001-234-567678";
 print_r (explode("-",$nStr));
?>
 
</body>
</html>

Third Example for convert string into an array using the limit parameter.

In the following example, we have declared a one string variable. With this declared variable, we will use the limit parameter of explode function

<?php
$string = "I love PHP and Laravel, Also love angular js";
 
 // without limit
print_r(explode(' ',$string));
print "<br>";
   
// zero limit
print_r(explode(',',$string,0));
print "<br>";
 
// positive limit
print_r(explode(',',$string,2));
print "<br>";
 
// negative limit 
print_r(explode(',',$string,-1));
?>  
 
//output
 
Array ( [0] => I [1] => love [2] => PHP [3] => and [4] => Laravel, [5] => Also [6] => love [7] => angular [8] => js )
 
Array ( [0] => I love PHP and Laravel, Also love angular js )
 
Array ( [0] => I love PHP and Laravel [1] => Also love angular js )
 
Array ( [0] => I love PHP and Laravel )

Source code of the limit parameter Of explode() In PHP

<!DOCTYPE html>
<html>
<title>Convert Number Strings to Arrays in PHP with Limit Parameter Example</title>
<head></head>
<body>
 
<h4>Break a number string into an array In PHP With Limit Parameter</h4>
 
<?php
$string = "I love PHP and Laravel, Also love angular js";
 
 // without limit
print_r(explode(' ',$string));
print "<br>";
   
// zero limit
print_r(explode(',',$string,0));
print "<br>";
 
// positive limit
print_r(explode(',',$string,2));
print "<br>";
 
// negative limit 
print_r(explode(',',$string,-1));
?>  
 
</body>
</html>

Conclusion

Convert strings into arrays in PHP using Explode() Function. You have learned how to convert string to an array with different types of delimiter of Explode() function in PHP