Convert a String into Array in PHP

PHP convert string into array; Through this tutorial, i am going to show you how to convert string into array with delimeters in PHP using explode() function.

PHP explode() function is used to convert a string or a comma-separated string into an array.

How to Convert a String Into Array in PHP

Use the below given examples to convert string into array using delimeters in PHP; as follows:

  • PHP convert string into an array
  • Convert string to array with delimiter php

PHP convert string into an array

Use the below given example to convert string to array with space delimiter php; is as follows:

<?php
$string = "I love PHP";
print_r (explode(" ",$string));
?>
 

The output of the above given code; is as follows:

 Array ( [0] => I [1] => love [2] => PHP )

Convert string to array with delimiter php

Use the below given example to convert string to array with – delimiter php; is as follows:

<?php
 $nStr = "001-234-567678";
 print_r (explode("-",$nStr));
?>
 

The output of the above given code; is as follows:

 
Array ( [0] => 001 [1] => 234 [2] => 567678 )

Recommended PHP Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.