Home  >  Article  >  Backend Development  >  How to convert string to array in php

How to convert string to array in php

青灯夜游
青灯夜游Original
2021-05-11 17:53:3718779browse

Conversion method: 1. Using the explode() function, you can use one string to split another string and return an array composed of strings. The syntax format is "explode('separator', string) "; 2. Use str_split() function, syntax "str_split (string)".

"How

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

Method 1: Use explode() Function

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
$str = &#39;PHP中文网,PHP教程,https://www.php.cn/,explode()函数,字符串转数组&#39;;
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
$arr = explode(&#39;,&#39;, $str);
print_r($arr);
$arr = explode(&#39;,&#39;, $str, 3);
print_r($arr);
$arr = explode(&#39;,&#39;, $str, -2);
print_r($arr);
$arr = explode(&#39;,&#39;, $str, 0);
print_r($arr); 
?>

Rendering:

"How

Description:

explode() function can be based on string delimiter Split a string, that is, it splits a string into several substrings based on the delimiter, and then combines these substrings into an array and returns it. The syntax format is as follows:

explode(separator,string,limit)

Parameters

  • delimiter: delimiter character used to split strings;

  • string: The string that needs to be split;

  • limit: Optional parameter, which can be empty, specifies the number of array elements to be returned;

    • If limit is not empty and is a positive number, the returned array contains at most limit elements, and the last element contains the remaining part of string;

    • If limit is not empty and If it is a negative number, all elements except the last limit elements will be returned;

    • If limit is 0, it will be treated as 1;

    • If limit is empty, it means returning all array elements.

If delimiter is an empty string "", the program will prompt a Warning, and the explode() function will return FALSE; if delimiter The contained value is not found in string and a negative limit is used, an empty array is returned, otherwise an array containing a single element of string is returned.

Method 2: Use str_split() function

<?php
$str=&#39;woxihuanphp&#39;;
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
$arr = str_split($str);
print_r($arr);
?>

"How

Description:

str_split() function separates characters Split the string into an array.

Syntax

str_split(string,length)
Parameters Description
string Required. Specifies the string to be split.
length Optional. Specifies the length of each array element. The default is 1.

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How 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