Reputation: 2095
is there any function to insert string1 into another string2 if known particular insert place of string2. For example I have HTML code about 2000 chars long. And at 1000 char I want to insert other string which is 200 chars length.
Your help would be appreciated.
Upvotes: 2
Views: 453
Reputation: 1754
use wildcards at the place you want to insert code
$html="<div>%%wildcard%%</div>"
and use str_replace
$new_html=str_replace('%%wildcard%%', 'i like cookies', $html);
Upvotes: 1
Reputation: 522024
substr($html, 0, 1000) . $newHtml . substr($html, 1000)
But actually, that sounds like a really fragile idea. You should rather use DOM processing methods.
Upvotes: 3
Reputation: 85458
Something like this would work:
$newstr = substr($str, 0, 1000) . $insert . substr($str, 1000)
Made into a function
function insertAt($str, $position, $toInsert) {
return substr($str, 0, $position) . $toInsert . substr($str, $pos)
}
Upvotes: 2