PHP : Replace first substring occurence

AcceuilCategory: PHPPHP : Replace first substring occurence
Kanzari Haithem Staff asked 2 months ago

Use the following function to replace first occurence of a substring in a given string :

public function str_replace_first($from, $to, $subject){
$from = '/'.preg_quote($from, '/').'/';
return preg_replace($from, $to, $subject, 1);
}