Explode Function in PHP
In this article, we will discuss PHP explode() function. It is used to splits a string into array elements. We must pass two arguments for this function, one is character and another one is array name. Let’s see the example code:
<?php
$str = 'red, green, blue, orange'; // define string
//convert string to array
$arr = explode(',' , $str);
print_r($arr);
?>
Output:
Array ( [0] => red[1] => green [2] =>blue [3]=> orange)
- In the above code, we have created a string “$str” and assigned some colors as values separated with a comma (,).
- If we want to convert that into an array, we need to use explode() function and pass a comma and $str.
- Then we print the variable “$arr”. See the above output, a string is converted to an array successfully.