[RANT]
OK, I think we are all guilty of abusing regular expressions from time to time purely for the sake of writing speed, but I think it’s important to take a few uses I have seen used as examples, and point out the correct method of achieving the desired effects under the given situation.
[B]/^[a-z]*$/
yes, there is a faster way of checking if you only use letters a-z, although it may take longer to type:
[code=php](strspn($STR,”abcdefghijklmnopqrstuvwxyz”) == strlen($STR))
what’s more is that this function becomes faster in relation to the regular expression the larger the list of valid characters becomes:
[code=php](strspn($STR, “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234556789”) == strlen($STR))
ranks significantly quicker than the related regular expression.
[b]/^[^@]{1,63}@[^@]{1,255}$/
very nice, test the length of 2 strings using a regular expression, never mind that the strlen function was created for this very thing, we can write a regular expression for it so it will all be fine. quite why the writer of this never thought that splitting the string on the @ symbol, then providing 3 checks of length(one on the array, 1 on each half of the result) I do not know, but although it sounds like a longer check it works out to STILL be more efficient.
[b]preg_replace(“/ />/”, “>”, $str)
sorry, but this is just silly, to remove the / just use a standard str_replace script.
[/RANT]
There are tons more example of this sort of abuse(being taught as if it should be done) online, but these are the worst offenders I could find(2 of which I only found this morning, congratulations roScripts.