⊗ppPmRgWSCh 247 of 447 menu

Checking the Entire String with Regular Expressions in PHP

Often preg_match is used to check if an entire string matches a regular expression. In this case, a caret ^ should be placed at the beginning of the regex, and a dollar sign $ at the end. This tells the engine that the entire string must match the pattern.

Let's find out, for example, if a string consists entirely of the letter 'a' or not:

<?php echo preg_match('#^a+$#', 'aaaa'); // outputs 1 echo preg_match('#^a+$#', 'aaab'); // outputs 0 ?>

Determine if the passed string is a domain. Use the following strings for tests:

<?php $arr[] = 'site.ru'; // + $arr[] = 'site.com'; // + $arr[] = 'my-site.com'; // + $arr[] = 'my-cool-site.com'; // + $arr[] = 'my_site.com'; // + $arr[] = 'site123.com'; // + $arr[] = 'site.travel'; // + $arr[] = 'si$te.com'; // - $arr[] = 'site.r'; // - ?>

Determine if the passed string is an email. Use the following strings for tests:

<?php $arr[] = 'addr@mail.ru'; // + $arr[] = 'addr123@mail.ru'; // + $arr[] = 'my-addr@mail.ru'; // + $arr[] = 'my_addr@mail.ru'; // + $arr[] = 'addr@site.ru'; // + $arr[] = 'addr.ru'; // - $arr[] = 'addr@.ru'; // - $arr[] = 'my@addr@mail.ru'; // - ?>
byenru