convert string to array in php

Generally we use PHP array for storing data, some time we need to convert a string into an array as well as array into a string.

PHP have some function to achieve this , i will provide you some example of that

php function that convert string into array

I will show you two ways to convert a string into an array

1 – explode() – convert string into an array using a single delimiter
2 – preg_split() – convert string into an array using regex

php function EXPLODE()

this php function converts a string into an array , String is split by delimiter specified by the first parameter passed to the function

Example

PHP function PREG_SPLIT()

Convert string to array using preg_split().

If you need to split a string into an array using regular expression

PHP function IMPLODE()

IMPLODE convert array to string using implode().

Example

implode(separator,array)

implode uses 2 parameters like separator and array

1 – separator Optional. Specifies what to put between the array elements. Default is “” (an empty string)
2 – array Required. The array to join to a string

Example

Leave a Reply

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