PHP: How to Convert String to an Array [explode()]

[ad_1]

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

  1. PHP Convert String to Array
  2. PHP Array: Indexed,Associative, Multidimensional
  3. To Remove Elements or Values from Array PHP
  4. PHP remove duplicates from multidimensional array
  5. Remove Duplicate Elements or Values from Array PHP
  6. Array Push and POP in PHP | PHP Tutorial
  7. PHP Search Multidimensional Array [key and value and return key

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00