PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Oct 20, 2018 9:06 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Sun May 27, 2012 1:53 pm 
Offline
Forum Contributor
User avatar

Joined: Fri Oct 06, 2006 8:12 pm
Posts: 294
I have some old code that has this:

Syntax: [ Download ] [ Hide ]
if (!ereg ("([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})", $startdate, $regs) || $startdate == '')


And of course it is now deprecated.

I tried simply replacing ereg with preg_match, but that didn't work. I suspect I need to escape the / character (it gives "Unknown modifier: '/'"), but I couldn't figure out how.


Last edited by The Phoenix on Mon May 28, 2012 9:55 am, edited 1 time in total.

Top
 Profile  
 
PostPosted: Sun May 27, 2012 2:05 pm 
Offline
Forum Contributor
User avatar

Joined: Fri Oct 06, 2006 8:12 pm
Posts: 294
Figured it out. My solution:

Syntax: [ Download ] [ Hide ]
if (!preg_match("/([0-9]{4})\/.([0-9]{1,2})\/.([0-9]{1,2})*/", $startdate, $regs) || $startdate == '')


Used / at start and */ at end, and then escaped the slash with \/.


Top
 Profile  
 
PostPosted: Sun May 27, 2012 7:03 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
You did more than just convert it to a PCRE expression. You've changed its meaning.

- To escape a character all you do is add a backslash. Periods mean "any character" and you've added two of them.
- If the starting delimiter is "/" then the ending delimiter is also a "/". An asterisk means "any amount of the previous thing" and you've added one of them to the last number set.
To demonstrate what I mean, try validating
Syntax: [ Download ] [ Hide ]
$startdate = "This string contains 9999/ 99/+987654321 which is clearly not a date";

which also shows a separate issue...


Top
 Profile  
 
PostPosted: Sun May 27, 2012 10:54 pm 
Offline
Forum Contributor
User avatar

Joined: Fri Oct 06, 2006 8:12 pm
Posts: 294
Ok, so do you have a suggestion on how to fix it correctly?

Based on your feedback, I thought
Syntax: [ Download ] [ Hide ]
if (!preg_match("/([0-9]{4})\/([0-9]{1,2})\/([0-9]{1,2})/", $startdate, $regs) || $startdate == '')

would be successful, but it seems to not be. Regex is not my strong suit. Further suggestions welcome.


Top
 Profile  
 
PostPosted: Sun May 27, 2012 11:39 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
Two parts. First is an expression:
Syntax: [ Download ] [ Hide ]
$validformat = preg_match('/^(\d\d\d\d)\/(\d\d?)\/(\d\d?)$/', $startdate, $regs);

That only validates the format of the string. Next comes the actual date validation:
Syntax: [ Download ] [ Hide ]
if ($validformat && $regs[1] >= 1900 && $regs[1] <= date("Y") + 1000 && checkdate($regs[2], $regs[3], $regs[1])) {
    // valid

The check with $regs[1] is to make sure it's a reasonable year.


Top
 Profile  
 
PostPosted: Mon May 28, 2012 9:55 am 
Offline
Forum Contributor
User avatar

Joined: Fri Oct 06, 2006 8:12 pm
Posts: 294
This was super helpful. Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group