Solved

PHP checking if string matches pattern

Posted on 2013-06-07
3
503 Views
Last Modified: 2013-06-07
Hello,

I would like to check if a string matches this pattern:

'yyyy-mm-dd hh:mm'

I am not sure how to do this will PHP.

Thanks in advance for any advice.

Cheers,
0
Comment
Question by:1Cougar
  • 2
3 Comments
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39228504
You can use a regular expression

preg_match('/[0-9{4}\-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}/', $string);

Will return 0 if no match or > 0 if a match.
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 39228514
Assumes 0 padded fields for month, day, hour and minute.

If you want to validate actual month / day values better to do that in code - working out leap years and different month values might be overly complex.

You can do this by modifying your preg_match call to

<?php
// RegEx pattern with () to save matched components into $matches array
$pattern = '/([0-9]{4})\-([0-9]{2})\-([0-9]{2}) ([0-9]{2}):([0-9]{2})/';
$string = '2012-11-21 12:45';
echo preg_match($pattern, $string, $matches);
echo "<pre>";
// $matches now holds yyyy, mm, dd, hh, mm in array values 1-5
print_r($matches);
echo "</pre>";
?>

Open in new window

0
 

Author Comment

by:1Cougar
ID: 39228578
Hi,

Thank you!  Your first answer works fine--all dates are 0 padded.

Many thanks for your help!
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
paypal ipn url 5 60
PHP not parsing ' character 12 40
PHP Syntax Error 4 27
How do uses indexes to maximize MySQL Searches 14 37
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question