How to Convert String to Array in PHP

How to Convert String to Array in PHP

Convert strings to arrays in PHP using explode() function. This tutorial teaches, how to convert a string of comma-separated strings into an arrays 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

ParameterDescription
separatorRequired. Specifies where to break the string
stringRequired. The string to split
limitOptional. 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

Leave a Reply

Your email address will not be published. Required fields are marked *