Simply add a number as the fourth parameter in preg_replace().
For example, I’ve limited the following code (from the link you provided) to removing replacing ONCE:
$first_img = str_replace('https://yourwebsite.com', '', $first_img,1 );
You are dealing with the “limit” parameter.
See more here: