phpstringmergeoverlapping

built in function to combine overlapping string sequences in php?


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.


Solution

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

    $string1 = 'abcde';
    $string2 = 'cdefgh';
    
    echo substr_replace($string1, $string2, strcspn($string1, $string2)); // abcdefgh