0

I need to check a string like 1234567 01-01-74 (without the quotes).

My string's first numeric value has to be 7 digits in length, followed by space, then a date string with - between the day, month and year digits.

How do I do that?

1
  • Should avoid this question because of regular expression fanboys.. but I'll try to help anyhow.. Does the date have to be valid? Commented Jul 24, 2011 at 12:26

1 Answer 1

3

If I understand your question correctly, the following regular expression should work:

(\d{7}) \d{2}-\d{2}-\d{2}

Using preg_match() we can test to see if a string is valid or not:

// The "i" after the pattern delimiter indicates a case-insensitive search
if(preg_match("/(\d{7}) \d{2}-\d{2}-\d{2}/i", "1234567 01-01-74")) 
{
    // Valid string code here
    echo "Valid";
}
else 
{
    // Bad string code here
    echo "Not valid! Ogblog!";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Not a problem Raj. Don't forget to mark this as correct and upvote if it all works out :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.