Bounce
Bounce

Reputation: 2095

Insert string into other string

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

Answers (3)

Christian Smorra
Christian Smorra

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

deceze
deceze

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

NullUserException
NullUserException

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

Related Questions