How to Convert a String to Array Using PHP Without Explode Function?

How to Convert a String to Array Using PHP Without Explode Function

We always encounter a situation where we need to convert a string to array. We can easily do the same with the help of explode() method in PHP, but what if we need to do the same thing without the use of explode() method.

PHP has many useful built-in functions, and I think that makes it very easy to use and user friendly. You will get built-in string functions for almost all the string or text related queries.

It has many Math functions for number related queries which can solve almost all types of number-related problems. Most importantly it has array functions that help developers a lot to solve their problems. Because we know that array are very important part of any web application.

We almost perform all things using array in the application like passing the data from one method to another, from one controller to another, from one view to another. So the array becomes very important and thus we need sometimes the conversion of String to Array, which we are going to explain below.

The str_split() is an inbuilt PHP function that is used to convert the string into an array. This function basically splits the given string into smaller strings of the length defined by the user and stores them in an array and returns the array.

Code to convert String to Array

In below example, we pass 3 to create an array containing 3 characters each.

$string = 'sldfjlsdkjfwerlwelrkjewr';

$split = str_split($string, 3);



    [0] => sld
    [1] => fjl
    [2] => sdk
    [3] => jfw
    [4] => erl
    [5] => wel
    [6] => rkj
    [7] => ewr

Conclusion :

We use most of the time explode method to convert text to an array. The above code, string into an array without uses of explode() function which is very easy to use. We use str_split() method to device the array by passing an argument into an array.

Please check How to block IP addresses from accessing the application

Spread the love

Leave a Reply

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