PHP: How to Convert String to an Array [explode()|str_split]

PHP: How to Convert String to an Array [explode()|str_split]

PHP: How to Convert String to an Array [explode()|str_split]. Convert String to Array using explode() explode() method is one of the built-in function in PHP which can be used to convert string to array. The explode() function splits a string based on the given delimiter. The method splits the string where the delimiter exists. Example: Convert comma separated string to array in PHP.

PHP provides various functions to convert a String to an Array. In this article, we will discuss 3 ways to convert string to array in PHP using 3 different methods:

  1. explode()
  2. str_split()
  3. str_word_count()

1) Convert String to Array using explode()

explode() method is one of the built-in function in PHP which can be used to convert string to array.

The explode() function splits a string based on the given delimiter. A delimiter acts as a separater and the method splits the string where the delimiter exists.

Example: Convert comma separated string to array in PHP

$string = "one,two,three,four";
$arr = explode(",", $string);
print_r($arr);

//output
Array ( [0] => one [1] => two [2] => three [3] => four )

In above example, the string contains four words separated by a comma. Normally, in csv files, each column values in a row is separated by comma.

So, to extract each value and store it in array, we are converting string to array using explode() function. The returned array will contain list of words as elements.

php

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire PHP Developer - Best PHP Web Frameworks for Web Development

Hire our Laravel, CodeIgniter, YII, Zend, Cake PHP, Core PHP developers for your custom web development projects. Choose best PHP Web Frameworks & get satisfactory results.

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...

Noteworthy PHP Development Tools that a PHP Developer should know in 2021!

![This is image title](https://telegra.ph/file/856eae74f33107c93ca2c.jpg "This is image title") Hypertext Preprocessor, commonly known as PHP, happens to be one of the most widely used server-side scripting languages for developing web...

Top PHP web development company| PHP development services India

Are you looking for a php web development company? ValueCoders is a leading software company with 16+ years of experience offering PHP website development services to global clients. 4200+ projects | 2500+ Clients| 450+ Experts.

Hire Dedicated Core PHP Developers | Hire PHP Developer

Our dedicated PHP developers write bug-free and optimized code for custom PHP web development projects. Hire us today and get satisfactory results.