Php splits and merges two string function instances. php string function instances _ PHP Tutorial

Source: Internet
Author: User
Php splits and merges two string function instances, and php string function instances. Php split and merge two strings function example, php string function example this article describes the php split and merge two strings function. Share it with you for your reference. For example, php splits and merges two strings.

This example describes the function of splitting and merging two strings in php. Share it with you for your reference. The specific implementation method is as follows:

In this example, the two strings are split and merged, for example, str1 = aaaa and str2 = bbbb. after the strings are merged, the abababab is generated.

/*** Merges two strings in a way that a pattern like ABABAB will be * the result. ** @ param string $ str1 String A * @ param string $ str2 String B * @ return string Merged string */function MergeBetween ($ str1, $ str2) {// Split both strings $ str1 = str_split ($ str1, 1); $ str2 = str_split ($ str2, 1 ); // Swap variables if string 1 is larger than string 2 if (count ($ str1)> = count ($ str2) list ($ str1, $ str2) = array ($ str2, $ str1); // Append the shorter string to the longer string for ($ x = 0; $ x <count ($ str1 ); $ x ++) $ str2 [$ x]. = $ str1 [$ x]; return implode ('', $ str2);} // sample demo: print MergeBetween ('abcdef ','__'). "\ n"; print MergeBetween ('_', 'abcdef '). "\ n"; print MergeBetween ('BB ', 'A '). "\ n"; print MergeBetween ('A', 'BB '). "\ n"; print MergeBetween ('A', 'B '). "\ n";/* Output: a_ B _cdefa_ B _cdefbabaababab */

I hope this article will help you with php programming.

Examples in this article describes the function of splitting and merging two strings in php. Share it with you for your reference. Implementation...

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.