php分割合并兩個字串的函數執行個體,php字串函數執行個體
本文執行個體講述了php分割合并兩個字串的函數。分享給大家供大家參考。具體實現方法如下:
這裡實現把兩個字串進行分割合并,例如str1=aaaa,str2=bbbb,合并後產生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_cdefa_b_cdefbabaababab*/
希望本文所述對大家的php程式設計有所協助。
http://www.bkjia.com/PHPjc/1019452.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1019452.htmlTechArticlephp分割合并兩個字串的函數執行個體,php字串函數執行個體 本文執行個體講述了php分割合并兩個字串的函數。分享給大家供大家參考。具體實現方...