How to convert string to array in php

青灯夜游
Release: 2023-03-09 16:02:01
Original
18711 people have browsed it

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)".

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

Method 1: Use explode() Function

'; $arr = explode(',', $str); print_r($arr); $arr = explode(',', $str, 3); print_r($arr); $arr = explode(',', $str, -2); print_r($arr); $arr = explode(',', $str, 0); print_r($arr); ?>
Copy after login

Rendering:

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)
Copy after login

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

'; $arr = str_split($str); print_r($arr); ?>
Copy after login

Description:

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

Syntax

str_split(string,length)
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!