Parsing of PHP segmentation and composition String functions

Source: Internet
Author: User
Split String

The segmentation of a string is achieved through the explode () function. The explode () function splits a string by the specified rules and returns an array of values.

The syntax format is as follows:

Explode (Separator,string,limit)

The parameter list for the function is as follows:

Parameters Description
Separator The necessary parameters, specifying the split identifier.
String necessary parameters, specifying the string to be split
Limit Optional parameter that specifies the number of array elements returned

Description: If separator is an empty string (""), Explode () returns false, and if separator contains a value that cannot be found in a string, the explode () function returns an array containing a single element of string.

If the limit parameter is set, the returned array contains a maximum of limit elements, and the last element will contain the remainder of the string, and if the limit parameter is negative, all elements except the last-limit element are returned.

Use the explode () function to implement a string segmentation, which implements the following code:

<?php $str = "php manual @html manual @css manual @java Manual"; $array = Explode ("@", $str);                  Use the @ Split string var_dump ($array);                            Output string Split result?>

As can be seen from the above code, when splitting the string $str, split with "@" as the split identifier, split into 4 array elements, and finally use the Var_dump () function to output the elements in the array.

The results of the operation are as follows:

Array (4) {

[0]=> string (9) "PHP manual"

[1]=> string (Ten) "HTML manual"

[2]=> string (9) "CSS Manual"

[3]=> string (Ten) "Java Manual"

}

Note: By default, the index of the first element of the array is 0, and for the knowledge of the array, refer to the Topic.alibabacloud.comArray array.

In addition to using the Var_dump () function, the output array element can also be output using the Echo statement, the difference being that the var_dump () function outputs an array column, and the Echo statement outputs a single element in the array, with the "Var_dump ($ Array); "Use the following code to replace the elements in the output array.

<?php$str = "PHP manual @html manual @css manual @java Manual"; $array = Explode ("@", $str);                  Use @ to split the string echo $array [0];                              The 1th element in the output array echoes $array [1];                              The 2nd element in the output array echoes $array [2];                              The 3rd element in the output array echoes $array [3];                              The 4th element in the output array?>

The output is:

PHP Manual HTML manual CSS manual java Manual

Description: The above two methods of output split string the performance of the results will be slightly different.

Composite string

The implode () function can combine the contents of an array into a new string.

The syntax format is as follows:

Implode (Separator,array)

The parameter separator is optional. Specifies what is placed between the elements of the array. The default is "" (an empty string). The parameter array is required and is to be combined into an array of strings.

Apply the implode () function to concatenate the contents of the array into a new string with the * delimiter, and the code for its instance is as follows:

<?php$str = "PHP manual @html manual @css manual @java Manual"; $array = Explode ("@", $str);                  Use @ to split the string $arr = Implode ("*", $array);                  Combines the array with * into a string echo $arr;                                   Output String?>

The output is:

PHP manual *html manual *css manual *java Manual

Description

The implode () function and the explode () function are two relative functions, one for synthesizing strings and one for separating strings.

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.