1

Is there a way in php to make two strings combine to one? I want to combine strings with the same size together?

$string1 = "apple"
$string2 = "block"
//FUNCTION STUFF HERE
$output = "abplpolcek";
1
  • In C# we'd call it 'Zipping' two strings together, since they act like a zipper (1 side is put in, then the other; rinse, lather, repeat). Commented Aug 21, 2012 at 10:29

7 Answers 7

6

You could try this:

$output='';
for($i=0;$i<strlen($string1);$i++)
{
    $output.=$string1[$i];
    $output.=$string2[$i];
}

echo $output;

Or you can write a simple function like this:

function funnyConcatStrings($str1, $str2)
{
    $output='';
    $leng=strlen($str1);
    if(strlen($str1)==strlen($str2))
    {
        for($i=0;$i<$leng;$i++)
        {
            $output.=$str1[$i];
            $output.=$str2[$i];
        }
    }
    else
    {
        $output='Strings were not equal.\n';
    }
    return $output;
}

// Use it like this:

$mashedString=funnyConcatStrings($string1, $string2);

// or

echo funnyConcatStrings($string1, $string2);
Sign up to request clarification or add additional context in comments.

4 Comments

You shouldn't have strlen() in the loop like that- it should be outside the loop as as it's not executed each iteration.
@VettelS I edited it again to make a nice function that replaces the initial answer.
I edited and put the strlen($str1) in the for loop, no need for that extra line. Also, why did you do $output='';? In a function that has no global variables it should have that value already.
@FrankPresenciaFandosn there is no need to check the length with each iteration - better to check it once and keep using that value, what if there were a billion iterations? That's a lot of wasted CPU cycles. As for the $output=''; it is so that you can use the .= without throwing warnings.
2
$str_length = 5;
$output = '';

for($i = 0; $i < $str_length; $i++)
{
    $output .= $string1[$i] . $string2[$i];
}

Comments

1

use for instance $string1[0] ( letter 'a' ) to access the first letter and make a for loop

3 Comments

@Ionut Flavius Pogacian : Dode, you do not have to post code for such an easy task, we are here to provide answers based on the question, when learning you make baby steps, it was clear he didn`t know how to get characters from string
But you agree that this is high-school algorithmics and I assume that every person has basic programming knowledge and should use his brain...
yes, basics, but clearly, he does not know how to do it; he needs code, not explanations; ok, changing to +1
1

Really easy;

$a = 'abcdef';
$b = 'ghijkl';

$l = strlen($a);

$s='';

for($i=0;$i<$l;$i++)$s .= $a[$i] + $b[$i];

echo $s;

Comments

1

1.) Check if the string have the same lengts with strlen

2.) Then you can iterate through the string and access them as an array

$string = 'test123';
echo $string[0] -> 't'

Then you can combine the string and safe them in a new variable.

1 Comment

Then you should read the question. "I want to combine equal sized strings together?". He will combine equal sized string. This don't mean clearly that the string have the same length. So you should check it first.
0

This will work for strings that are different lenght as well

$string1 = "apple";
$string2 = "block";

$arr1 = str_split($string1);
$arr2 = str_split($string2);

if(count($arr1) > 0)
{
    foreach($arr1 as $key => $value)
    {
        $_tmp[] = $value;
        if(isset($arr2[$key]))
        {
            $_tmp[] = $arr2[$key];
        }
    }
}
else
{
    $key = 0;
}

if($key + 1 < count($arr2))
{
    for($i = $key + 1; $i < count($arr2); $i++)
    {
        $_tmp[] = $arr2[$key];
    }
}

echo implode("", $_tmp);

1 Comment

string is already an array of chars... $x = 'string'; echo $x[2]; # r
0
echo str_shuffle("apple" . "block");

result: aekbplopcl

1 Comment

You need to provide an explenation as well, even if it is (quite) trivial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.