PHP function example to split and merge two strings, PHP string function example_PHP tutorial

WBOY
Release: 2016-07-13 09:49:30
Original
1056 people have browsed it

PHP function example for splitting and merging two strings, php string function example

This article describes the php function for splitting and merging two strings. Share it with everyone for your reference. The specific implementation method is as follows:

Here, two strings are divided and merged, for example, str1=aaaa, str2=bbbb, and abababab

is generated after merging.
/**
 * 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
*/

Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1019452.htmlTechArticlephp function example of splitting and merging two strings, php string function example This article tells the example of php splitting and merging two strings string function. Share it with everyone for your reference. Specific implementation method...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template