Programs

String to Array in PHP: List of Functions Used

PHP is a powerful scripting language used for creating static, dynamic, and interactive web pages. It is embedded in HTML and manages web applications, databases, and session tracking.

A string is an array of characters. PHP works on strings and converts them to arrays with the help of some built-in functions.

In this article, we will look at some of the important functions of converting a string to an array in PHP. We will look at each PHP function and see how it is written in the program.

Functions used to Convert String to Array in PHP

There are basically four functions to convert String to Array in PHP:

  1. str_split()
  2. preg_split()
  3. chunk_split()
  4. explode()
  • The str_split function splits a string into array elements with the same length.
  • The preg_split function specifies the delimiter and controls the resultant array with the help of a regular expression.
  • The explode function splits the string when a delimiter is found.
  •  The chunk_split() function splits a string into smaller parts without altering the original string.

Let’s implement each of these functions and see how they convert String to Array in PHP.

1. str_split()

str_split function converts a string to an array in PHP. It splits into bytes, rather than characters with a multi-byte encoded string.

Syntax

str_split ( $string , $length )

Parameters

String – The given Input string.

Length – The maximum string length.

If the length is mentioned, the returned array breaks down into chunks of that length number, else, the chunk length of each will be by default, one character.

If the measure of length is more than the length of the string, the complete string returns as the first array element.

Example

<?php

$str = “PHP String ”;

$arr1 = str_split($str);

$arr2 = str_split($str, 3);

print_r($arr1);

print_r($arr2);

?>

Output

It will be in the form of Array

Array

(

[0] => P

[1] => H

[2] => P

[3] => 

[4] => S

[5] => t

[6] => r

[7] => i

[8] => n

[9] => g

[10] => 

)

Array

(

[0] => PHP

[1] =>  St

[2] => rin

[3] => g

)

2. preg_split()

preg_split is a function that splits a string using a regular expression.

Syntax

preg_split ( $pattern ,  $subject ,  $limit ,  $flags )

Parameters

Pattern – The string pattern to be searched.

Subject – The input string.

Limit – Substrings up to the limit value specified. A -1 or 0 means “no limit”.

Flags – If this flag is set, the appendant string offset is returned.

Example

<?php

$string = preg_split(“/[\s,]/”, “hypertext programming language “);

print_r($string);

?>

Output

Array

(

[0] => hypertext

[1] => programming

[2] => language

)

3. chunk_split()

chunk_split is a function that splits a string into a smaller chunk and returns a chunked string as an output.

Syntax:

chunk_split( $string , $length , $separator = “\r\n” )

Parameters

String: The string to be chunked.

Length: The length of the chunk.

Separator: The ending line sequence.

Example

<?php

$str = “Hello World!”;

echo chunk_split($str,1,“.”);

?>

Output

H.e.l.l.o. .w.o.r.l.d.!.

4. explode()

 explode is a PHP function that splits a string by a string. It is faster than the preg_split() function and is binary-safe.

Syntax:

explode ( $separator ,$string ,$limit)

Parameters

Separator: The string at the boundary.

String: The input string.

Limit: Specifies the length of the output string

If the limit is set to be a positive value, the returned array will be the maximum limit of elements with the last element having the remnant string.

If the limit is a negative value, all the string components excluding the last –limit are returned.

If the limit is zero, then it is assumed as 1.

The explode function splits the string parameter on boundaries limited by the separator and returns an array. If the separator is an empty string (“), it returns a false value.

If the separator consists of some value that is not the part of the string and a negative limit is used, an empty array is returned, else an array of the string will be returned.

Example

<?php

$input1 = “hello”;

$input2 = “hello,world”;

$input3 = ‘,’;

print_r( explode( ‘,’, $input1 ) );

print_r( explode( ‘,’, $input2 ) );

print_r( explode( ‘,’, $input3 ) );

?>

Output

array(1)

(

[0] => string(5) “hello”

)

array(2)

(

[0] => string(5) “hello”

[1] => string(5) “there”

)

array(2)

(

[0] => string(0) “”

[1] => string(0) “”

)

Conclusion

String to Array in PHP is a very important operation as it splits a string into an array of characters. In this article, we have demonstrated the process of string conversion to an array with the help of four PHP functions: str_split, preg_split, chunk_split and explode. We have also shown the example for each function showing how it is performed and will give an output. 

Programming with PHP needs skills to be learned and is a must for every PHP programmer. upGrad’s Master of Science in Computer science is a carefully created course where you will learn in-demand skills and perceive growth in your Software Development career journey.

Become a Full Stack Developer

UPGRAD AND IIIT-BANGALORE'S EXECUTIVE PG PROGRAMME IN FULL STACK SOFTWARE DEVELOPMENT
LEARN MORE @ UPGRAD

0 replies on “String to Array in PHP: List of Functions Used”

Accelerate Your Career with upGrad

Our Popular Software Engineering Courses

×