Mr. Lavalamp
Mr. Lavalamp

Reputation: 1888

How to insert text without replacing anything in PHP

How can I insert text into a PHP string at a specific point without overwriting anything?

Upvotes: 4

Views: 440

Answers (2)

Benjamin
Benjamin

Reputation: 61

Simple, use substr():

$str_a = "foo bar";
$str_b = substr($str_a, 0, 3) . ' baz' . substr($str_a, 3);

Upvotes: 0

Mr. Lavalamp
Mr. Lavalamp

Reputation: 1888

Use substr_replace() with a length value of 0.

CODE:

substr_replace("abcdefgh","-bbbb-",3,0); 

The output of the above will be "abc-bbbb-defgh"

Upvotes: 3

Related Questions