PHP (regular expression) - what is it? Examples and regular expression validation

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 regex




PHP

PHP - «ereg», «mb_ereg» «preg». «preg», PCRE, Perl, PHP. Preg- , .





regular expressions examples




, . .

PHP , . . , , . .

, /\d{3}-\d{2}-\d{2}/m «/», , «m» .

. - «\». (. . ). «|», . :

^
(
)
{
}
\d0 9
\D,
\s, ,
\w

PHP, , , .





PHP «». :

  1. . , (||) «», «-» «». , «» «».

  2. «» . , , . . : ((|)(|)) - : « ». , : « », «», «».

()

. , . , ? .

– . :

  • , ;
  • .

, : {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, :

  1. @ .
  2. , @, . .
  3. , . , SQL-. .
  4. , .
  5. .

, , .

php regular expressions email




URL

. , URL. , URL- .

. :

/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/

, .

url regular expressions




.1URL
.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. :

regular expressions examples




, .

. .

. , , , . . . :

+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, . , :

regular expression space




-

email regular expression




- , «» . - , . . , , .

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.




All Articles