substr vs regex

Hakim Cassimally hakim.cassimally at gmail.com
Mon Sep 3 10:54:34 BST 2007


On 03/09/07, alex at owal.co.uk <alex at owal.co.uk> wrote:
> Imagine, say, someone wanted the last three characters of a string. They
> might use a regex /(...)$/ or substr($variable, -3)
>
> personally I recommend the former because it is more readable, but others
> have recommended substr to because you "dont need to bring out the big
> guns". (However no one could actually remember the substr function)

I think the substr is arguably more readable.  maybe /(.{3})$/ is
clearer in intent (but it's still kind of ugly...)

> Is one of these significantly more efficient than the other? Ok ok, so I
> could probably write a test in a few minutes but asking you guys is more
> interesting.

Meh, I wouldn't even bother benchmarking it unless it came up as a
candidate for profiling...

-
osfameron


More information about the london.pm mailing list