PHP - How to split a string into an array - Functions explode, preg_split

  • Last updated: Nov 3, 2023
  • Views: 380
  • Author: Admin
PHP - How to split a string into an array - Functions explode, preg_split

Hello colleagues.

Every web developer in his robot will sooner or later face such a task as splitting a string into an array in the PHP programming language. One example of why you might want to split a string into an array is when you get a string from a database about a user's date of birth, and you need to split this string into a year, month, and day using some sort of delimiter.

In this article, we will talk about what are some simple functions built into PHP itself that will help you split a string into an array without any difficulties. In my example, I will split the string into an array using a space separator as the passed function parameter.

 

Article content:

  1. PHP explode function.
  2. PHP function preg_split.
  3. Results.

 

1. PHP explode function.

The first function we'll talk about is called explode. The function splits the string into an array with a delimiter. The explode function takes two required parameters and one optional, the first parameter is the split with which the php interpreter will understand how to split the string into an array, and the second parameter is the string itself. The function, as you already understood, returns an array.

Function syntax.

explode(string $separator, string $string, int $limit = PHP_INT_MAX): array

Example.

<?php
$text = 'I love php programming language';
$array = explode(' ', $text);
?>

php string to array

As you can see in the example, my string is split into an array using a space separator as the first parameter.


 

2. PHP function preg_split.

The second function to help us split a string into an array is called preg_split. The function splits a string into an array, specifying a regular expression. The preg_split function takes four parameters, two required and two optional parameters. In the first parameter, we need to specify the so-called template by which the string will be split into an array, and the second parameter is the string itself. If the parameters are passed correctly, then the function will return an array, otherwise it will return false.

Function syntax.

preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array|false

Example.

<?php
$text = 'I love php programming language';
$array = preg_split('/\s/', $text);
?>

php string to array

As you can see in this example, my string is split into an array using the pattern \s which means space.


 

3. Results.

As a result, colleagues, today we considered the possibility of splitting a string into an array using simple functions built into the php language itself. I recommend that you use the explode function in your projects because it will be faster than the preg_split function.


 

Thank you all, I hope my article was of some help to you.

SIMILAR ARTICLES

YII2 defaultRoute - How to change default controller in template

YII2 defaultRoute - How to change default controller in template

Pure HTML/CSS search bar

Pure HTML/CSS search bar

HTML/CSS - Expandable Searchbar Animation

HTML/CSS - Expandable Searchbar Animation