Home  >  Article  >  Backend Development  >  How to use the explode() function in php

How to use the explode() function in php

清浅
清浅Original
2019-01-18 13:34:423772browse

explode function is to split the string into different strings and return an array, and then retrieve each part of the string by accessing the array

PHP is a powerful website One of the development tools, it contains various built-in functions for various purposes, among which the explode() function is a built-in function mainly used to split a string into different strings.

How to use the explode() function in php

【Recommended course: PHP course

Meaning:

explode() function splits a string based on the string delimiter, that is, it splits the string into positions that come out of the delimiter. This function returns an array containing the string formed by splitting the original string, we can easily retrieve each part of the string by accessing the array

Its syntax structure is as follows:

explode(separator,string,limit)

separator: Indicates the key point at which the specified string will be split. In other words, as long as this character is found in the string, it will represent the end of one element of the array and the beginning of another element.

OriginalString: Represents the input string split in an array.

NoOfElements: used to specify the number of elements in the array. This parameter can be any integer (positive, negative or zero),

Positive number: means that an array containing at most limit elements will be returned

Negative number: means that the last N elements of the data will be Trimmed off, the remainder of the array will be returned as a single array

Zero: Indicates that the returned array will have only one element, the entire string

Example:

<?php
$str = &#39;hello,how, are ,you &#39;;

// 零 limit
print_r(explode(&#39;,&#39;,$str,0));

// 正的 limit
print_r(explode(&#39;,&#39;,$str,3));
// 负的 limit
print_r(explode(&#39;,&#39;,$str,-1));
?>

The rendering is as follows:

How to use the explode() function in php

Use specific characters as separators:

<?php
$str = &#39;he-llo-how-are you &#39;;

// 零 limit
print_r(explode(&#39;-&#39;,$str,0));

// 正的 limit
print_r(explode(&#39;-&#39;,$str,3));
// 负的 limit
print_r(explode(&#39;-&#39;,$str,-1));
?>

The rendering is as follows:

How to use the explode() function in php

Summary: The above is the entire content of this article. I hope this article can help everyone have a certain understanding of the explode() function.

The above is the detailed content of How to use the explode() function 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
Previous article:What is the MVC patternNext article:What is the MVC pattern