PHP splits and connects strings using functions such as explode (), implode (), and join (), and explodeimplode

Source: Internet
Author: User

PHP splits and connects strings using functions such as explode (), implode (), and join (), and explodeimplode
PHP uses functions such as explode (), implode (), and join () to separate and connect strings.

Generally, we want to view each part of the string. For example, you can view words in a sentence (for example, spelling check), or split a domain name or email address into component parts. PHP provides several string functions (and a regular expression function) to implement this function.

Split string: explode () function. Its function prototype is as follows:

array explode(string separator,string input[,int limit]);

This function carries an input string as a parameter, and splits the string itself into small parts based on a specified separator string, and returns the small parts to an array. You can use the optional parameter limit to limit the number of small pieces of strings. (This function is case sensitive)

Connection string: implode () function. Its function prototype is as follows:

string implode(string separator,array input);

Use implode () or join () functions to achieve the opposite effect of the function explode (). The effects of these two functions are consistent. Retrieve the array elements from the array and connect them together with the first input parameter characters. The call of this function is very similar to explode (), but the effect is opposite.

Example:

<? Php $ str = "He told me: 'Hello world! But I don't have any money! '"; Echo" original string: <br> "; var_dump ($ str); $ ret_arr = explode (" e ", $ str ); echo "Use the character 'e' to break down the string: <br>"; var_dump ($ ret_arr); $ ret_arr_3 = explode ("e", $ str, 3 ); echo "Use the character 'e' to break down the string and divide it into three blocks: <br>"; var_dump ($ ret_arr_3); $ ret_str = implode ("I", $ ret_arr_3 ); echo "concatenate a string array with the character 'I': <br>"; var_dump ($ ret_str );

Output:

Original string: string 'He told me: 'Hello world! But I don't have any money! ''(Length = 53) use the character 'e' to break down the string: array 0 => string 'H' (length = 1) 1 => string 'told m' (length = 7) 2 => string ': 'H' (length = 3) 3 => string 'llo world! But I don't hav '(length = 26) 4 => string 'any mon' (length = 8) 5 => string 'y! ''(Length = 3) use the character 'e' to break down the string and divide it into three blocks: array 0 => string 'H' (length = 1) 1 => string 'told m' (length = 7) 2 => string': 'Hello world! But I don't have any money! ''(Length = 43) concatenate a string array with the character 'I': string 'Hi told mi: 'Hello world! But I don't have any money! ''(Length = 53)
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Http://blog.csdn.net/sinat_36329815/article/details/78335649

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.