PHP String to Array Conversion

PHP String to Array Conversion with Examples – 4 Easy Ways

PHP String to Array Conversion

Hello Everyone, in this tutorial we will learn how can we do “PHP String to Array Conversion”. So let’s get started.

In various situation we may need to convert String to Array in PHP. When you have a string but want to store that string in the database as a Array or there are some transformation you want to do based on that string, you may need to transform that string to array.

There are several methods.

  1. explode() function
  2. preg_split()
  3. str_split
  4. str_word_count

But easiest one is using the explode function. The explode() function return a array from a given string. Too much talking, lets jump into the coding part ( show me the code 😉 ).

  1. explode function:  PHP explode function converts an string to an array. It takes three parameter. First two is required and one is optional.
    explode(separator, string, limit).
    Lets see an example:

    <!DOCTYPE html> 
    <html> 
    
    <body> 
    
    <?php 
    
    	$str = "PHP is Awesome!"; 
    
    	print_r (explode(" ",$str)); 
    
    ?> 
    </body> 
    
    </html>

    Output:

     

    Array ( [0] => PHP [1] => is [2] => Awesome )

     

  2. preg_split function:  preg_split() is a PHP function that uses regular expression to convert a string to array. It usually takes four (4) parameters. First two is required and second two is optional –  “preg_split(pattern, string, limit, flags)” .
    Lets see an example:

    <pre> 
    <!DOCTYPE html> 
    <html> 
    <body> 
    <?php 
    	$favourite_fruites = "Apple, Orange, Grape, Dates, Banana"; 
    
    	$pattern = "/ /"; 
    
    	$favourite_fruites_in_array = preg_split($pattern, $favourite_fruites); 
    	
    	print_r($favourite_fruites_in_array); 
    ?> 
    </body> 
    </html> 
    </pre>

     

    Output:

     

    Array ( [0] => Apple, [1] => Orange, [2] => Grape, [3] => Dates, [4] => Banana )

     

  3. str_split function:  PHP str_split() function is little different. It does not take any separator. This function takes two parameter. first one is the string you want to convert to array, second parameter is optional. Second parameter can be used to define the length of split. Default value of second parameter is  1. If nothing is provided, str_split() will transform the string into array with individual letters and spaces.
    str_split(string, length).
    Lets see an example:

    <!DOCTYPE html>
    <html>
    <body>
     
    <?php
    	print_r(str_split("How you doin!"));
    ?>
     
    </body>
    </html>

    Output:

     

    Array ( [0] => H [1] => o [2] => w [3] => [4] => y [5] => o [6] => u [7] => [8] => d [9] => o [10] => i [11] => n [12] => ! )

     

  4. str_word_count function:  PHP str_word_count function by default count words in an array. It takes three parameter. First one is required, which is the string to count. Second one is return type. Possible values are: 0,1,2.
    • 0: By default it is 0, which returns the word count of string.
    • 1: Returns an array with the words found
    • 2: Returns an array where the key is the position of the word in the string, and value is the actual word

    Third parameter is char, also “Optional”. This parameter specifies special characters to be considered as words.

    Function: str_word_count(string, return, char)
    Lets see an example:

    <!DOCTYPE html> 
    <html> 
    <body> 
    <?php 
    
    	print_r(str_word_count("Hello PHP Ninja!",1)); 
    
    ?> 
    </body> 
    </html>

     

    Output:

    Array ( [0] => Hello [1] => PHP [2] => Ninja )

     

    That’s all for today. See you in next tutorial.

Summary

You can also read about PHP, Laravel, MySQL 

Our YouTube Channel: WebPlug

I hope you get an clear idea about how to convert string to array in php ion 4 different ways. Hope this tutorial will be helpful. If you want to learn about PHP Array to String Conversion, check this tutorial:  Array To String Conversion See you in next one.  

I would like to have feedback : info@laravelplug.com
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

 

Popular Article:

Method Chaining in PHP: https://laravelplug.com/php-method-chaining-in-oop/

Laravel Traits: https://laravelplug.com/laravel-traits-in-action/

Laravel Log Viewer: https://laravelplug.com/laravel-log-viewer/

Editorial Staff

A Learner and trying to share what I learned!