Convert string or comma separated string to array in PHP; In this tutorial, we will learn how to convert a string, comma separated strings to an array in PHP using explode().
We should also read this PHP array and string tutorials:
How to Convert a String to Array in PHP
Basically, the PHP explode() function is used to split or break strings to substrings by a given delimiter(comma, $, #, @, ! etc) in PHP. And it 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.
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
Let’s see the following examples of convert string or comma seperated stringto array in PHP using explode() function:
- Example 1 – Convert string into an array
- Example 2 – String converted to a string
- Example 2 – String converted to a string
Example 1 – 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>
Example 2 – 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>
Example 3 – 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 exploding 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; we have learned how to convert strings to an array with different types of delimiters of Explode() function in PHP.
Recommended PHP Tutorials
- PHP Convert String to Array
- PHP Array: Indexed,Associative, Multidimensional
- To Remove Elements or Values from Array PHP
- PHP remove duplicates from multidimensional array
- Remove Duplicate Elements or Values from Array PHP
- Array Push and POP in PHP | PHP Tutorial
- PHP Search Multidimensional Array [key and value and return key