Home >Backend Development >PHP Tutorial >How to use the explode() function in php
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.
【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 = 'hello,how, are ,you '; // 零 limit print_r(explode(',',$str,0)); // 正的 limit print_r(explode(',',$str,3)); // 负的 limit print_r(explode(',',$str,-1)); ?>
The rendering is as follows:
Use specific characters as separators:
<?php $str = 'he-llo-how-are you '; // 零 limit print_r(explode('-',$str,0)); // 正的 limit print_r(explode('-',$str,3)); // 负的 limit print_r(explode('-',$str,-1)); ?>
The rendering is as follows:
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!