views:

27

answers:

2

Is there a built in function in PHP that would combine 2 strings into 1?

Example:

$string1 = 'abcde';
$string2 = 'cdefg';

Combine to get: abcdefg.

If the exact overlapping sequence and the position are known, then it is possible to write a code to merge them.

TIA

A: 

No, there is no builtin function, but you can easily write one yourself by using substr and a loop to see how much of the strings that overlap.

Emil Vikström
Thanks, I am just looking for a short cut to cut down on the code. But I guess it is not possible.
Jamex
A: 

It's possible using substr_replace() and strcspn():

$string1 = 'abcde';
$string2 = 'cdefgh';

echo substr_replace($string1, $string2, strcspn($string1, $string2)); // abcdefgh
Alix Axel
Thanks, I will give it a try, it should help shorten my codes.
Jamex