serbestgezer Posted September 21, 2009 Share Posted September 21, 2009 I am trying to replace second character in the string with x when I use str_replace it replace it all "a" with x. Is there any way to replace second character in the string? <?php $subject = "baaia"; $body = str_replace($subject[2], "x", $subject); echo $body; // bxxix I want to print like "bxaia" ?> Link to comment https://forums.phpfreaks.com/topic/174960-solved-str_replace-help/ Share on other sites More sharing options...
Garethp Posted September 21, 2009 Share Posted September 21, 2009 $body = preg_replace('~(.).(.*)~s', "$1x$2", $subject); Link to comment https://forums.phpfreaks.com/topic/174960-solved-str_replace-help/#findComment-922093 Share on other sites More sharing options...
serbestgezer Posted September 21, 2009 Author Share Posted September 21, 2009 thanks mate Link to comment https://forums.phpfreaks.com/topic/174960-solved-str_replace-help/#findComment-922094 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.