Replace only first match using preg_replace [duplicate]

The optional fourth parameter of preg_replace is limit:

preg_replace($search, $replace, $subject, 1);

Leave a Comment