Comparing a string with a given pattern

I take input from the user in the form field. I want to make sure that this input must be in a template like "2012-xxx-111", i.e. The first four must be integers, and after that there must be a “-” sign, followed by two or three alphabets, followed by a “-” and at the end any integer consisting of 3 numbers. Help me do all this in php. Thanks your help will be appreciated.

+4
source share
2 answers

Use preg_match with regex

if (preg_match("/^[0-9]{4}-[A-Za-z]{2,3}-[0-9]{3}$/", "Search String Here")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
+2
source

You can use a regular expression for this.

[0-9]{4}-[A-Za-z]{2,3}-[0-9]{3}

0 9 , -, 2 3 a z, , , - 3 .

http://regexr.com/3bo81

PHP preg_match(), , .

+3

Source: https://habr.com/ru/post/1606418/


All Articles