PHP implements explore() function sample code

怪我咯
Release: 2023-03-13 21:46:02
Original
2653 people have browsed it

explode() function breaks up thestringinto an array. Returns an array of strings

Syntax

explode(separator,string,limit)
Copy after login

Parameters Description
separator Required. Specifies where to split the string.
string Required. The string to split.
limit

Optional. Specifies the number of array elements to be returned.

Possible values:

  • Greater than 0 - Returns an array containing up tolimitelements

  • Less than 0 - Returns an array containing all but the last -limitelements

  • 0 - Returns an array containing one element

#Note: The "separator" parameter cannot be an empty string.

The sample code is as follows:

''); $len = strlen($str); $slen = strlen($seg); $_limit = 0; for($i=0; $i<$len; $i++){ if(substr($str,$i,$slen) == $seg ){ $_limit++; $i += $slen-1; continue; }else{ $_ret[$_limit] .= $str[$i]; } } if($limit < 0 ) $_ret = array_slice($_ret, 0 , $limit ); else{ $_ret = $limit >= count($_ret) ? $_ret : array_merge(array_slice($_ret, 0 , $limit-1 ), array(implode( $seg , array_slice($_ret,$limit-1)))); } return $_ret; } var_dump(myExplode("4",$str)); echo "
"; var_dump(explode("4",$str)); echo "
"; ?>
Copy after login

Supports limit to be negative. . The delimiter isstring

The above is the detailed content of PHP implements explore() function sample code. 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!