Hello Everyone,
Hoping for a bit of guidance and place to start on the following coding issue.
I have a variable that is a string of delimited numbers:
$myvar = '1|4|5|21|5|6|8';
I want my PHP script to look for a specific number in $myvar and then update it in sequence with a marker directly after the searched for number. I was using str_replace to accomplish this as follows:
//Old variable to be updated
$myvar = '1|4|5|21|5|6|8';
//Number to search for
$search = '1';
//Marker
$marker = 'm';
//Use str_replace to create an updated variable
$newvar = str_replace($search, $search . '|' . $marker, $myvar);
The issue with this code is that it works for the first '1' within the delimited variable, but also replaces any other number with a '1' in it. I've considered blowing the variable up into an array and the doing the replacement there, but am concerned with the overhead this would cause. I believe preg_replace would probably be a less intensive process, but I'm having a difficult time writing a regex that would search and replace correctly. Any help is greatly appreciated!
Thanks!