PHP split to combine two strings of function instance detailed

Source: Internet
Author: User
This article mainly introduces the PHP division combined with two strings of the function, an example of PHP for string manipulation of the relevant skills, the need for friends can refer to the following

The example in this paper describes the function of PHP splitting and merging two strings. Share to everyone for your reference. The implementation method is as follows:

Here the implementation of the two string to split the merge, such as STR1=AAAA,STR2=BBBB, after the merge generated Abababab

/** * Merges the strings in a-a-to-a pattern like ababab would be * the result. * * @param string $str 1 string A * @param string $str 2 string B * @return String merged String */function Mergebe  Tween ($str 1, $str 2) {//Split both strings $str 1 = str_split ($str 1, 1);  $str 2 = Str_split ($str 2, 1); Swap variables if string 1 is larger than String 2 if (count ($str 1) >= count ($str 2)) List ($str 1, $str 2) = Array (  $str 2, $str 1);  Append the shorter string to the longer string for ($x =0; $x < count ($str 1); $x + +) $str 2[$x]. = $str 1[$x]; Return implode (", $str 2);} Example Demo: Print Mergebetween (' abcdef ', '). "\ n";p rint mergebetween (', ' abcdef '). "\ n";p rint mergebetween (' BB ', ' AA '). "\ n";p rint mergebetween (' aa ', ' BB '). "\ n";p rint mergebetween (' A ', ' B '). "\ n";/*output:a_b_cdefa_b_cdefbabaababab*/

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.