Forum Moderators: coopster

Message Too Old, No Replies

Use of {} in PHP 5.3 String Test Functions

         

jillsybte

6:13 pm on Apr 2, 2010 (gmt 0)

10+ Year Member



To ensure I'm ready for my host to upgrade to PHP 5.3, I am trying to update some PHP forms where I used eregi for validating email addresses. I have switched to preg_match and that's working fine right now with 5.2.9. I noticed one note on the PHP 5.3 deprecation page (http://php.net/manual/en/migration53.deprecated.php). It states, "The use of {} to access string offsets is deprecated. Use [] instead."

I use the following for my email validation test:
'/^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$/i'

At the end where it tests the length of the TLD, should I change {2,4} to [2,4]?

Thanks,
Jill

rocknbil

6:49 pm on Apr 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard jillsybte, no, they are talking about a different context of {}. PHP preg is based on the Perl regular expression engine, in the context of a regexp it's perfectly "legal."

In your regexp,

[a-z]{2,4}$

means the string ends ($) with anything from two to four letters (case insensitive.) If you were to do this,

[a-z][2,4]$

it would mean the string ends with any single letter next to a single 2, comma, or 4, like

a2 would match
b, would match
f4 would match

a2, would NOT match, because of the end of string character
bb would NOT match, needs single character and there's no 2, comma, or 4
,b would NOT match, needs letter next to 2, comma, or 4

jillsybte

7:53 pm on Apr 2, 2010 (gmt 0)

10+ Year Member



Thanks so much, rocknbil. I had just found an example of {} used for access string offset so I had a better idea of what it means. Plus your examples have now made it crystal clear. Thanks again.