Home » PHP » Split String in PHP by delimiter

Split String in PHP by delimiter

Split a string and get all values into an array in PHP. Suppose you are getting a lengthy string separated by comma, pipe or slash or it could be any delimiter. And you want to split and read every part of that delimiter to use it for other task. To do this use explode function.  Explode function is used to split string in PHP by delimiter.

Here is the syntax for explode function:

array explode (string $delimiter ,string $string [,int $limit = PHP_INT_MAX])

Where array is the name of array in which explode function will split the string. String $delimiter is the delimiter for example "," and string $string it the string you want to split.

Below is the example to split string in PHP by delimiter using explode function.

<?php
// Example
$field_list = "item_code, item_name, desc, category, department";
$fields = explode(",", $field_list);
echo $fields[0]; // item_code
echo $fields[1]; // item_name
?>

For full reference you can check the following link: http://php.net/manual/en/function.explode.php

1 thought on “Split String in PHP by delimiter”

Comments are closed.