【问题标题】:How to write a program to concatenate two strings character by character. e.g : JOHN + SMITH = JSOMHINTH如何编写一个程序来逐个字符地连接两个字符串。例如:约翰 + 史密斯 = JSOMHITH
【发布时间】:2017-11-24 21:41:32
【问题描述】:

这是我的代码,但它仅在两个字符串长度相同时才会给出结果。但是如果我想比较不同长度的字符串呢?

<?php

  $n1 = "Apple";
  $n2 = "Orang";

  //count length fo both string 
  $count1 = strlen($n1);
  $count2 = strlen($n2);
  $finalcount = 0;

  if($count1 > $count2) {
    $finalcount = $count1;
  } elseif ($count1 < $count2) {
    $finalcount = $count2;
  } else {
    $finalcount = $count1;
  }

  //convert string to array
  $n1 = str_split($n1);
  $n2 = str_split($n2);

  $i = 0;
  $result = "";
  for($i = 0;$i < $finalcount  ; $i++) {
    $result = $result .$n1[$i] . $n2[$i];
  }

  echo $result;
?>

【问题讨论】:

  • 代码什么时候进入else部分?
  • 分享一个示例输入以及输出应该是什么。你的代码没什么意义

标签: php string compare


【解决方案1】:

这是一种方法(cmets 中有一些解释):

<?php

$str = 'test';
$str2 = 'test2';

$arr = str_split($str);
$arr2 = str_split($str2);

// Find the longest string
$max = max(array(strlen($str), strlen($str2)));

$result = '';

for($i = 0; $i < $max; $i++){
    // Check if array key exists. If so, add it to result
    if (array_key_exists($i, $arr)){
        $result .= $arr[$i];
    }

    if (array_key_exists($i, $arr2)){
        $result .= $arr2[$i];
    }
}

echo $result; //tteesstt2

?>

【讨论】:

  • 不使用预定义函数..可以这样做吗?以及如何做到这一点?
  • @hearthacker “不使用预定义函数” 是什么意思?此代码中没有任何预定义的函数可以解决手头的问题。您在此处看到的每个“预定义函数”都只是普通的 PHP,它所做的事情与问题本身完全无关。这只是导致预期结果的所有因素的组合。
【解决方案2】:

这正是您要查找的内容: PHP - Merge two strings

/**
 * 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);
}

【讨论】:

    【解决方案3】:
    <?php 
    
    // Write a program to concatenate two strings character by 
    // character. e.g : JOHN + SMITH  = JSOMHINTH
    
    echo "\nstring 1 : ".$string1="JOHN";
    echo "\nstring 2 : ".$string2="SMITH";
    
    $firstArray=str_split($string1);
    $secondArray=str_split($string2);
    
    if(count($firstArray)>count($secondArray)) {
      $loop=count($firstArray);
    }
    else {
      $loop=count($secondArray);
    }
    
    $result=[];
    $i=0;
    while($i<$loop) {
      isset($firstArray[$i])?array_push($result,$firstArray[$i]):'';
      isset($secondArray[$i])?array_push($result,$secondArray[$i]):'';
      $i++;
    }
    echo "\nResult String : ".implode($result);
    
    ?>
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2014-04-02
    • 2019-01-14
    • 1970-01-01
    • 2011-11-04
    • 2022-11-16
    • 2016-10-13
    相关资源
    最近更新 更多