Deimantas Brandišauskas
Deimantas Brandišauskas

Reputation: 1772

PHP - preg_match for car license plate number(and letters)

I seen this post in other question(PHP preg_match car license plate) about how to validate if a user actually writes something in the form field to validate if it's actually right. I mean for ex. in my case I need what my real car license plate number to be AAA-111 , 3 character, dash and 3 numbers. This is what I founded in StackOverflow to match 9a-4e-yy return preg_match[0-9A-Za-z]{2}(-[0-9A-Za-z]{2}){2}$ , but I need AAA-111 and it should return in function so I can use it in other classes. Thank you for any help.

Upvotes: 0

Views: 3160

Answers (1)

Gaia
Gaia

Reputation: 2952

Use /[A-Za-z]{3}-[0-9]{3}/. This is case insensitive.

You could also upper case the string and run it thru /[A-Z]{3}-[0-9]{3}/.

Here is the PHP code

<?php
$pattern = '/[A-Za-z]{3}-[0-9]{3}/';
$subject = 'aaa-111';
$result = preg_match( $pattern, $subject , $matches );
echo $result;
print_r($matches);
?>

Upvotes: 2

Related Questions