Jump to content

replace "-" with "/" in preg_match pattern


dadamssg

Recommended Posts

I found this preg_match() function which searches the parts of a string and uses the checkdate() function to see if it is a date. It expects the format of the date to be "YYYY-MM-DD". I would like to rewrite it to expect the format to be "MM/DD/YYYY".

 

<?php
function checkDateFormat($date)
{
  //match the format of the date
  if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
  {
    //check weather the date is valid of not
        if(checkdate($parts[2],$parts[3],$parts[1]))
          return true;
        else
         return false;
  }
  else
    return false;
}

 

I'm pretty sure the following is write to change the string length of each part but i don't know how to change the expected "-"'s to be a "/"'s.

 

<?php
if (preg_match ("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $date, $parts))

 

Any help would be greatly appreciated!

Link to comment
Share on other sites

to clarify, the function expects a delimiter for the pattern, a designated character at the start and end of the pattern.  This is because modifiers are also placed inside the string (after the ending delimiter).  You can use most any non-alphanumeric character as the delimiter, but whatever you choose has to be escaped if you want to use it in the actual pattern. 

 

So for example, in your OP you have / as the delimiter, but wanted to use that character in your pattern.  So you could have kept / as the delimiter and just escaped the / in your pattern by doing \/  I personally like using ~ because it rarely comes up in patterns and I hate escaping.  But just sayin'...

 

Also, I would also like to point out that you don't actually need to use regex for this..you can explode at the /, count the elements exploded (to make sure there are only 3) and then put them in that checkdate.

Link to comment
Share on other sites

Another option:

 

<?php
function checkDateFormat($date){
@list($m, $d, $y) = explode("/", $date);
if(empty($m) || empty($d) || empty($y) || !checkdate($m, $d, $y))
	return false;
return true;
}

var_dump(checkDateFormat("13/2012"));
var_dump(checkDateFormat("cat"));
var_dump(checkDateFormat("2012-05-05"));
var_dump(checkDateFormat("2012/05/05"));
var_dump(checkDateFormat("12/05/1205"));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.