Related operations that explain the orderly splitting of PHP strings

Source: Internet
Author: User
The contents of an ordered split of PHP strings are important in PHP, and this article describes the operation of an ordered split of PHP strings.

Here are some of the functions

Chunk_split (): A function splits a string into a series of smaller parts.

Explode (): Splitting another string with one string

Str_split (): Splitting a string into an array

Chunk_split ()

Chunk_split (String,length,end)

Parameter description

string is required. Specifies the string to be split.

Length is optional. A numeric value that defines the length of the string block. The default is 76.

End is optional. A string value that defines what is placed at the end of each string block. The default is \ r \ n.

<!--? PHP$STR = "Shanghai"; Echo Chunk_split ($str, 1, ".");? -

Input result: S.h.a.n.g.h.a.i.

Explode ()

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

Array explode (string separator, string string [, int limit])

<!--? php$str = ' One|two|three|four ';p rint_r (Explode (' | ', $STR));p Rint_r (Explode (' | ', $STR, 2));//negative limit (from PHP 5.1) Print_r (Explode (' | ', $STR,-1));? -

The output results are as follows:

Array (    [0] = one    [1] = [    2] = = Three    [3] = four) array (    [0] = = one    [1] = t Wo|three|four) Array (    [0] =    One [1] =    [2] = three) Str_split () Str_split () splits the string into an array, An array was returned successfully. Array Str_split (string string [, int length])

Parameter description

String that needs to be split

Length optional, representing the lengths of each split unit, not less than 1

Example:

<!--? Php$str = ' One Three ', $arr 1 = str_split ($STR), $arr 2 = Str_split ($str, 3);p Rint_r ($arr 1);p Rint_r ($arr 2);? -

The output results are as follows:

Array (    [0] = = o    [1] = n    [2] = + E    [3] = =     [4] = [5] +    W    [6] = = O    [ 7] =     [8] = = [    9] = h    [ten] = R    [one] ~ ~ E [A] +    e) Array (    [0] = one
  [1] =  tw    [2] = = O T    [3] = HRE    [4] = = e)

This article explains the sequential splitting of PHP strings related operations, more relevant content please focus on PHP Chinese web.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.