When working with texts in any modern programming language, developers are constantly confronted with the tasks of checking the entered data for compliance with the desired template, finding and replacing test fragments, and other typical operations for processing symbolic information. The development of our own verification algorithms leads to a loss of time, incompatibility of program code and complexity in its development and modernization.
WEB- . PHP. . .
PHP
PHP - «ereg», «mb_ereg» «preg». «preg», PCRE, Perl, PHP. Preg- , .
, . .
PHP , . . , , . .
, /\d{3}-\d{2}-\d{2}/m «/», , «m» .
. - «\». (. . ). «|», . :
PHP, , , .
PHP «». :
. , (||) «», «-» «». , «» «».
«» . , , . . : ((|)(|)) - : « ». , : « », «», «».
()
. , . , ? .
– . :
, : {2,5}. , : {2,}.
. :
1. * - , {0,}.
2. + - , . . {1,}.
3. ? – - {0,1}.
, , - . , . PHP 4.x . . « », PHP, Python, Perl, MySQL, Java, Ruby C#.
E-mail
. -, email. . , . .
. , PHP email- , . , , .
, , email, :
- @ .
- , @, . .
- , . , SQL-. .
- , .
- .
, , .
URL
. , URL. , URL- .
. :
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
, .
.1 | URL |
.2 | «http» |
.3 | |
.4 | «s», URL «https» |
.5 | «//» |
.6 | |
. 7-9 | |
.10-13 | |
.14-17 | URL — , , , , |
. . Visa MasterCard.
. . . , (. . ).
, , , , . /D, , .
. , , . , – . Visa 4 13 16 . MasterCard 51-55 16. :
, .
. .
. , , , . . . :
+CCC.NNNNNNNNNNxEEEE, :
- C – , 1-3 .
- N – 14 .
- E – .
, .
:
^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$
. . , .
. :
1 24 | ^(1[0-2]|[1-9])$ |
1-31 | ^(3[01]|[12][0-9]|[1-9])$ |
0-59 | ^[1-5]?[0-9]$ |
1 100 | ^(100|[1-9]?[0-9])$ |
1-366 | ^(36[0-6]|3[0-5][0-9]|[12][0-9]{2}|[1-9][0-9]?)$ |
IP-
. , IP- IPv4 000.000.000.000-255.255.255.255.
. PHP, . , :
-
- , «» . - , . . , , .
But completely trusting the results of online services is not recommended for all developers using PHP. A regular expression, written and verified personally, enhances qualifications and ensures that there are no errors.