If I have a string like \"test\", I have characters from offset 0-3. I would like to add another string to this one at offset 6. Is there a simple PHP function that can do this?
To solve your problem first of all you convert your strings in arrays with str_split
, then when you've got the array you're done, you can perform any kind of operations on these strings.
Code:
$s1 = "test";
$s2 = "new";
//converting string into array
$strings[0] = str_split($s1, 1);
$strings[1] = str_split($s2, 1);
//setting the first word of sentence
$sentence = $strings[0];
//insert every character in the sentence of "new" word
for ($i=0; $i < count($strings[1]); $i++) {
$sentence[] = $strings[1][$i];
}
print_r($sentence);
Result:
Array
(
[0] => t
[1] => e
[2] => s
[3] => t
[4] => n
[5] => e
[6] => w
)