views:

37

answers:

2

Need to replace a char from another string.

$s1='123456789';
$s2='abcdefghi';

$p=4; // position of char in $s1 to use for replacing (0 is first char)

$s2 = ???? ; // code

In the end $s2 must be 'abcd5fghi'

What would be fastest method?

+3  A: 

String access and modification by character

$s2[$p] = $s1[$p];
Lekensteyn
+5  A: 

If you only have single-byte characters:

$s2[$p] = $s1[$p];

Otherwise, in case of multi-byte characters, you will probably need to use mb_substr:

$s2 = mb_substr($s2, 0, $p).mb_substr($s1, $p, 1).mb_substr($s2, $p+1);
Gumbo
I learn something new. Thanks for saving me time writing the crap long version.
Yehonatan