How to insert a substring at specific position in a string in PHP

2 Answers

0 votes
$s = "Let every user this";
$pos = strpos($s, 'user');
$s =  substr_replace($s, ' access', $pos + strlen('user'), 0);
echo $s;


/*
run:

Let every user access this

*/

 



answered Aug 3, 2015 by avibootz
0 votes
$s = "Let every user this";
$pos = strpos($s, 'user');
$s = substr($s, 0, $pos + strlen('user')) . ' access' . substr($s, $pos + strlen('user'));
echo $s;


/*
run:

Let every user access this

*/

 



answered Aug 4, 2015 by avibootz

Related questions

2 answers 269 views
3 answers 195 views
3 answers 273 views
1 answer 210 views
1 answer 182 views
...