Php string splitting Function

Source: Internet
Author: User

In the php tutorial, two common functions are used to split strings: chunk_split and explode. There is also a str_split function. See the instance below.
Definition and usage
The chunk_split () function Splits a string into a series of smaller parts.

Syntax
Chunk_split (string, length, end) parameter description
String is required. Specifies the string to be split.
Length is optional. A number that defines the length of a string block.
End is optional. String value, which defines the content placed after each string block.

*/

$ Data = "hello world! This is a world! "; // Define a string
$ New_string = chunk_split ($ data); // splits the string
Echo $ new_string; // output result

/*
Definition and usage
The explode () function separates strings into arrays.

Syntax
Explode (separator, string, limit) parameter description
Separator is required. Specifies where to split the string.
String is required. The string to be split.
Limit is optional. Specifies the maximum number of returned array elements.

*/

$ Str = 'one | two | three | four '; // define a string
$ Result = explode ('|', $ str, 2); // cut the string
Print_r ($ result); // output result
$ Result = explode ('|', $ str,-1); // return the number with a negative number.
Print_r ($ result); // output result

/*
Definition and usage
The str_split () function separates strings into arrays.

Syntax
Str_split (string, length) parameter description
String is required. Specifies the string to be split.
Length is optional. Specifies the length of each array element. The default value is 1.

*/

$ Str = "hello world"; // define a string
$ Result = str_split ($ str); // perform the conversion operation.
Print_r ($ result); // output the converted result
$ Result = str_split ($ str, 4); // each element is set to 4
Print_r ($ result); // output the converted result


?>

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.