Home > Backend Development > PHP Tutorial > Implementation steps of orderly splitting of php strings

Implementation steps of orderly splitting of php strings

php中世界最好的语言
Release: 2023-03-17 18:48:01
Original
2719 people have browsed it

Today I will tell you a few functions. Let us work together to realize the steps of orderly splitting of php string. Please see the case below

chunk_split() :函数把字符串分割为一连串更小的部分。
explode():使用一个字符串分割另一个字符串
str_split():将字符串分割到数组中
chunk_split()
Copy after login

Grammar

chunk_split(string,length,end)
Copy after login

Parameter Description

string Required. Specifies the string to be split.

length Optional. Numeric value defining the length of the string block. The default is 76.

end Optional. A string value that defines what to place at the end of each string block. The default is \r\n.

<!--?php
$str = "Shanghai";
echo chunk_split($str,1,".");
?-->
输入结果:S.h.a.n.g.h.a.i.
explode()
Copy after login

This function is the inverse function of implode(). It uses one string to split another string and returns an array.

array explode( string separator, string string [, int limit] )
Copy after login

Parameter Description

separator split flag

string The string to be split

limit is optional, indicating that the returned array contains at most limit elements. The last element will contain the remaining part of the string, supporting negative numbers.

<!--?php
$str = &#39;one|two|three|four&#39;;
print_r(explode(&#39;|&#39;, $str));
print_r(explode(&#39;|&#39;, $str, 2));
// 负数的 limit(自 PHP 5.1 起)
print_r(explode(&#39;|&#39;, $str, -1));
?-->
Copy after login

The output results are as follows:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)
Array
(
    [0] => one
    [1] => two|three|four
)
Array
(
    [0] => one
    [1] => two
    [2] => three
)
str_split()
Copy after login

str_split() splits the string into an array and returns an array successfully.

?
1
array str_split( string string [, int length] )
Copy after login

Parameter Description

string The string to be divided

length is optional, indicating the length of each division unit, which cannot be less than 1

Example:

<!--?php
$str = &#39;one two three&#39;;
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?-->
Copy after login

The output results are as follows:

Array
(
    [0] => o
    [1] => n
    [2] => e
    [3] =>  
    [4] => t
    [5] => w
    [6] => o
    [7] =>  
    [8] => t
    [9] => h
    [10] => r
    [11] => e
    [12] => e
)
Array
(
    [0] => one
    [1] =>  tw
    [2] => o t
    [3] => hre
    [4] => e
)
Copy after login

I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:

Answers to questions about camel case naming and JS

How to use JS bubbling events

Detailed explanations and examples of Boolean values, relational operators, and logical operators in JS

The above is the detailed content of Implementation steps of orderly splitting of php strings. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template