I have the following piece of code:
$Page = str_replace('-&-', '-[+]-', $Page);
$Page = str_replace('&', '-[+]-', $Page);
$Page = str_replace('-and-', '-[+]-', $Page);
$Page = str_replace('-et-', '-[+]-', $Page);
$Page = str_replace('-und-', '-[+]-', $Page);
$Page = str_replace('-y-', '-[+]-', $Page);
$Page = str_replace('-&-', '-[+]-', $Page);
which I then run many times per page load (perhaps 10 to 40 times).
N.B.
$Page
represents a URL fragment - rarely more than 20 characters long.
Am I better off leaving it as it is, or may I, for the purposes of code maintenance, rewrite this code as:
$Page = preg_replace('/(-&-|-&-|&|-and-|-et-|-und-|-y-)/', '-[+]-', $Page);
without suffering a slowdown of one or several hundredths of a second?
(If the slowdown is a matter of milliseconds, I am less bothered).
Background:
I am less familiar with the evolution of PHP than with that of javascript, css etc. so when I see recommendations (long-stated, albeit from the late 2000s and early 2010s) like:
Avoid
preg_replace
unless you need to use it and always usestr_replace
instead, which is an order of magnitude faster.
I don't know if that recommendation still has any relevancy for PHP 7.3 in 2020, or whether that recommendation was important to follow in the days of PHP 5, but is less of a concern now (given that PHP 7 is so much faster and more optimised than its predecessor).