What PHP function does user nikosch recommend using instead of str_replace in this context?

The issue with using str_replace in this context is that it replaces all occurrences of the search string with the replacement string, which may not be the desired behavior. To replace only the first occurrence, user nikosch recommends using the PHP function preg_replace with a limit parameter set to 1.

// Original string
$string = "Hello, World! Hello, Universe!";

// Replace only the first occurrence of "Hello" with "Hi"
$newString = preg_replace('/Hello/', 'Hi', $string, 1);

echo $newString; // Output: "Hi, World! Hello, Universe!"