I need to replace a char in a 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?
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);
$s2[$p] = $s1[$p];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.