On the contrary. People are parrotting the regex are evil meme way too often IMO. It's obvious that preg_match is overused in
php, but it's less obvious that it's oftentimes sensible to do so (in PHP).
I would go so far and conjecture that it's yet another microoptimization in php land to use the string functions. There are many and many useful, and they are usually the better choice. But you shouldn't shun
preg_match in favour of multiple
if chains. Because in practice it turns out, libpcre is often faster than PHP can execute a loop looking for string alternatives e.g.
As a recent example made me realize, testing if a string is all-lowercase:
if ($string == strtolower($string))
Is more readble than:
if (!preg_match("/[A-Z]/", $string))
And you would assume the first must be faster, since it's all-PHP. But in reality the regex only looks over the string once, and can abort the negated condition as soon as it finds an uppercase letter. The strtolower() approach however looks over the string twice. First strtolower() makes a string duplicate by iterating over each letter, comparing and uppercasing it. Then the
== iterates over the original and the copy again, comparing them once more.
So that's not an obvious case. And to be objective the first one is often faster, since you normally just compare short strings. But it's imperative to not go blindly by the assumption that PHP string functions are always advisable over regular expressions.
(I'm tempted to add another rant about @bobince's fun answer regarding xhtml-regexes, and how it's recently often linked in a very unhelpful manner. And the more objective answers below go ignored.)