This article mainly introduces PHP methods for string function operations. Interested friends can refer to it. I hope it will be helpful to everyone.
The example of this article describes the function of splitting and merging two strings in PHP. The specific implementation method is as follows:
Here, two strings are split and merged, for example, str1=aaaa, str2=bbbb , after merging, abababab
/** * 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); } //范例演示: print MergeBetween('abcdef', '__') . "\n"; print MergeBetween('__', 'abcdef') . "\n"; print MergeBetween('bb', 'aa') . "\n"; print MergeBetween('aa', 'bb') . "\n"; print MergeBetween('a', 'b') . "\n"; /* Output: a_b_cdef a_b_cdef baba abab ab */
is generated. Summary: The above is the entire content of this article. I hope it will be helpful to everyone's study.
Related recommendations:
PHP’s method of implementing a ring queue based on memcache
PHP uses curl to send post, get requests and operate cookies
The above is the detailed content of PHP methods for string function operations. For more information, please follow other related articles on the PHP Chinese website!