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?
I am trying this, but getting an error:
PHP Fatal error: Cannot use assign-op operators with overloaded objects nor string offsets in ...
I understand I could concatenate these strings, but I want to build a sentence based on output from Stanford CoreNLP that provides string offset locations http://nlp.stanford.edu/software/example.xml (more info at http://nlp.stanford.edu/software/corenlp.shtml)
$strings[0] = "test";
$strings[1] = "new";
foreach($strings as $string) {
for($i = 0 ; $i <= strlen($string); $i++) {
print $string[$i];
if (!isset($sentence)) {
$sentence = $string[$i];
}
else {
$sentence[strlen($sentence)] .= $string[$i];
}
}
}
print_r ($sentence);
PHP docs say at http://www.php.net/manual/en/language.types.string.php
Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits E_NOTICE. Negative offset emits E_NOTICE in write but reads empty string. Only the first character of an assigned string is used. Assigning empty string assigns NULL byte.