Php string segmentation (explode str_split preg_split

Source: Internet
Author: User

Use "str_split" to separate strings. The advantage is that a space is also used as an array element. In my previous example, the previous string contains two spaces, and the next string has only one space. However, the output displays the same information. It can also be split by other separators, such as "explode" or "preg_split ",

Use "str_split" to separate strings. The advantage is that a space is also used as an array element. In my previous example, the previous string contains two spaces, and the next string has only one space. However, the output displays the same information.
It can also be split by other separators, such as "explode" or "preg_split ",


Php tutorial explode () function
Php string Function
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.

Example
In this example, we split the string into an array:

$ Str = "hello world. it's a beautiful day .";
Print_r (explode ("", $ str ));
?>

Output:

Array
(
[0] => hello
[1] => world.
[2] => it's
[3] =>
[4] => beautiful
[5] => day.
)

Str_split split Function

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.

Description
If the length is less than 1, The str_split () function returns false.

If the length is greater than the length of the string, the entire string is returned as a unique element of the array.
Example
Example 1

Print_r (str_split ("hello "));
?>

Output:

Array
(
[0] => h
[1] => e
[2] => l
[3] => l
[4] => o
)

Example 2

Print_r (str_split ("hello", 3 ));
?>

Output:

Array
(
[0] => el
[1]


Preg_split -- use a regular expression to split a string
Description
Array preg_split (string pattern, string subject [, int limit [, int flags])


Returns an array that contains the substrings separated by subject along the boundary matching pattern.

If the limit parameter is specified, a maximum of limit substrings are returned. If the limit parameter is-1, there is no limit. It can be used to specify the optional parameter flags.

Flags can be any combination of the following tags (using bitwise OR operator | combination ):


Preg_split_no_empty
If this flag is set, preg_split () returns only non-null components.


Related Article

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.