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?

+3


source to share


2 answers


Use substr_replace () with a length value of 0.

CODE:



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

      

The output above would be "abc-bbbb-defgh"

+2


source


Simple, use substr()

:



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

      

0


source







All Articles