Solution:2
You could use str_replace
if you know the full value you want to replace:
$find = "2547: Second Result";
$replace = "2547: My New Value";
$haystack = "2548: First Result|2547: Second Result|2550: Third Result|2551: Fourth Result";
$result = str_replace($find, $replace, $haystack);
print_r($result);
You can also use a loop 😱:
$collection = [];
$find = "2547: Second Result";
$haystack = "2548: First Result|2547: Second Result|2550: Third Result|2551: Fourth Result";
$parts = explode('|', $haystack);
foreach ($parts as $part) {
[$id, $old_value] = explode(':', $part);
if (strpos($find, $id) !== false) {
$part = "{$id}: My New Value";
}
$collection[] = $part;
}
$result = implode('|', $collection);
print_r($result);
These are not the only ways to do this…