DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 16th August 2008
cajunman4life cajunman4life is offline
Real Name: Aaron Graves
Package Pilot
 
Join Date: May 2008
Location: Coolidge, Arizona
Posts: 203
Default PHP regular expression help

Hey guys, writing a regex for PHP to validate date input to be put in an entry in MySQL.

Format must be YYYY-MM-DD HH:MM:SS

I have this:

Code:
  $must_match = "^[0-9]{4}\-[0-9]{2}\-[0-9]{2}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}$";
  if(preg_match($must_match, $dte) && $dte) {
    if(!$dte) {
      $query = "INSERT INTO $table VALUES ('', NOW(), 'ajgraves', '$post');";
    } else {
      $query = "INSERT INTO $table VALUES ('', '$dte', 'ajgraves', '$post');";
    }
  } else {
    die("ERROR: You entered an invalid date");
  }
  mysql_query($query);
And no matter what I enter, it always fails. Any ideas?
__________________
I just saved a bunch of money on my car insurance by fleeing the scene of the accident!
Reply With Quote
  #2   (View Single Post)  
Old 16th August 2008
cajunman4life cajunman4life is offline
Real Name: Aaron Graves
Package Pilot
 
Join Date: May 2008
Location: Coolidge, Arizona
Posts: 203
Default

UPDATE: The code above was borked in that leaving the field blank would always fail. I at least fixed that:

Code:
  $must_match = "^[0-9]{4}\-[0-9]{2}\-[0-9]{2}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}$";
  if((preg_match($must_match, $dte) && $dte) || (!$dte)) {
    if(!$dte) {
      $query = "INSERT INTO $table VALUES ('', NOW(), 'ajgraves', '$post');";
    } else {
      $query = "INSERT INTO $table VALUES ('', '$dte', 'ajgraves', '$post');";
    }
  } else {
    die("ERROR: You entered an invalid date");
  }
  mysql_query($query);
But if I actually enter a date, the correct format fails (so do incorrect formats, which is the intended effect )

I'm thinking it has to do with maybe the way I check for the space? "\ " perhaps isn't the best way. Maybe someone more versed in this can help me
__________________
I just saved a bunch of money on my car insurance by fleeing the scene of the accident!
Reply With Quote
  #3   (View Single Post)  
Old 16th August 2008
cajunman4life cajunman4life is offline
Real Name: Aaron Graves
Package Pilot
 
Join Date: May 2008
Location: Coolidge, Arizona
Posts: 203
Default

DOH! I fixed it! I forgot two "/"'s. It should look like this:

Code:
$must_match = "/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}$/";
All that was needed to be changed was add "/" in front of "^" and after "$". I hate simple mistakes. Let this be a lesson to everyone else.
__________________
I just saved a bunch of money on my car insurance by fleeing the scene of the accident!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expressions: renaming files with 'sed' J65nko Guides 26 15th October 2010 08:03 PM
perl expression syntax qsecofr Programming 3 16th February 2009 12:56 PM
Mount filesystem with a regular user ivanatora FreeBSD General 15 30th July 2008 08:51 AM
are you an former bsdforum regular? ephemera Off-Topic 18 28th July 2008 12:57 PM


All times are GMT. The time now is 05:06 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick