Home >Backend Development >PHP Problem >What is the function to convert string to array in php

What is the function to convert string to array in php

青灯夜游
青灯夜游Original
2021-03-15 17:58:358390browse

The functions in php that convert strings to arrays include: 1. str_split() function, syntax "str_split(string,length)"; 2. explode() function, which can return a string array; 3. preg_split() function.

What is the function to convert string to array in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Convert a string to an array in PHP The functions used are str_split(), explode(), preg_split().

PHP str_split() function

str_split() function splits a string into an array.

Syntax

str_split(string,length)

What is the function to convert string to array in php

Return value:

If length is less than 1, the str_split() function will return FALSE. If length is greater than the length of the string, the entire string will be returned as the only element of the array.

Example:

<?php
$str="Hello";
var_dump($str);
$arr=str_split($str);
var_dump($arr);
?>

What is the function to convert string to array in php

PHP explode() function

explode() function uses a String splits another string and returns an array of strings.

Syntax

explode(separator,string,limit)

What is the function to convert string to array in php

Return value: Returns a string array.

Example:

<?php
$str="Hello world !";
var_dump($str);
$arr=explode(" ",$str);
var_dump($arr);
?>

What is the function to convert string to array in php

PHP preg_split() function

preg_split function separates characters by a regular expression string.

Syntax

array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )

Separate the given string by a regular expression.

Parameter description:

What is the function to convert string to array in php

Return value

Returns an array composed of substrings obtained after using pattern boundaries to separate subject.

Example:

<?php
$str = &#39;hypertext language programming&#39;;
var_dump($str);
$chars = preg_split(&#39;/ /&#39;, $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
var_dump($chars);
?>

What is the function to convert string to array in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the function to convert string to array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn