Merge two strings
Merges two strings in a way that a pattern like ABABAB will be the result.
/** * 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); }
Snippet Details
-
AuthorJonas John
-
LicensePublic Domain
-
LanguagePHP
-
Created12/05/2007
-
Updated12/05/2007
-
Tagsstring functions
Sorry folks, comments have been deactivated for now due to the large amount of spam.
Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!
Older comments:
anonimous April 19, 2011 at 05:12
you are awesome
thanks, but i was not looking for this (but i will help me one day)
thanks, but i was not looking for this (but i will help me one day)
Roland February 26, 2009 at 11:37
Why swap variables if str1 is larger? It has no logic... Example #2 should output:
"_a_bcdef"
"_a_bcdef"