FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » CTYPE
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: CTYPE [message #182673 is a reply to message #182669] Sun, 25 August 2013 20:42 Go to previous messageGo to previous message
Fiver is currently offline  Fiver
Messages: 35
Registered: July 2013
Karma:
Member
On 2013-08-25 21:28, Twayne wrote:
> On 2013-08-25 2:38 PM, J.O. Aho wrote:
>> You know you will end up with loads of if cases if you keep on doing
>> this instead of using a regular expression, this would speed it up and
>> make your code a lot easier to read (case insensitive):
>>
>> if(preg_match("/^[a-z]\d[a-z] \d[a-z]\d$/i",$postcode)) {
>> // this could be a valid Canadian postcode
>> // just add what you need to do to check it against
>> // Canadian Post, worst case use the "Find a post office"
>> }

> I agree with you but I'm not useing regular expressions. 1 they're never
> clear to me, 2 I'm trying to avoid them as I have a couple in other
> cases where they actually cause noticeable slow-downs on even my Local
> Server, and magnitudes worse with I upload them to be "live", 3 there
> are so many ways to do the same thing in PHP my goal is to learn them
> and take notes of the most useful.
> So it's not just learning to code properly, it's also to learn
> better, less-code ways of doing things.

Your original question was answered, but it seems you have a few
misconceptions about regular expressions. If you into learning how to do
things properly, you shouldn't dismiss them out of hand.

ad 1) "they're never clear to me"

That's a fair point, as regexes can quickly become hard to read. There's
a way around that, however: the /x modifier. Take for example the regex
in the post you replied to (just as an example, I'm not judging its
correctness for now):

preg_match("/^[a-z]\d[a-z] \d[a-z]\d$/i", $postcode)

This can also be written as

$regex = '/
^ # anchored at the start of the string
[a-z] # one letter a-z
\d # one digit 0-9
[a-z] # one letter a-z
\s # one character of white space *
\d # one digit 0-9
[a-z] # one letter a-z
\d # one digit 0-9
$ # anchored at the end of the string
/ix';

preg_match($regex, $postcode)

Much easier to understand this way, especially if you're starting out
with regular expressions. That's all one string - all the white space
and the comments are ignored. (Talking about white space, a more exact
way to match "one space character" would be "[ ]").

ad 2) "they're too slow"

That may have been true in other languages and many, many years ago, but
it's certainly not the case with PHP today. This regex runs over 2
million times per second on a single core of my middle-aged laptop. It
should be more than fast enough, considering I could check every
Canadian's zip code in under 15 seconds with it... I don't know what
you're building, but I can guarantee that this won't be the reason for
any noticeable slow-downs.

ad 3) "there are many other ways"

True, but the regex is by far the shortest. It's also what most
programmers would expect to see in this situation. It is, as you say, a
"better, less-code" way to do it.

[snip]

> I dislike saying all those things about myself

I found it interesting to read.

regards,
5er
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: korean character sets
Next Topic: Android app Developers requirements
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 12:14:24 GMT 2024

Total time taken to generate the page: 0.03634 seconds