PHP split a function instance with two strings, PHP string function instance _php Tutorial

Source: Internet
Author: User

PHP split a function instance combining two strings, PHP string function instance


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-strings in a-a-to-a pattern like ababab would be is * the result. * * @param   string  $str 1  string A * @param   string  $str 2  string B * @return  string  Merged String */function Mergebetween ($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];< C17/>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*/

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1019452.html www.bkjia.com true http://www.bkjia.com/PHPjc/1019452.html techarticle PHP Split Merge two strings of function instances, PHP String Function Example This article describes the PHP split merge two strings of the function. Share to everyone for your reference. Concrete implementation of the party ...

  • 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.