PHP Lesson 26 – Regular expressions and their Functions

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

PHP Lesson 26 – Regular expressions and their Functions - PHP Lesson 26 – Regular expressions and their Functions - PHP Lesson 26 – Regular expressions and their Functions - php regular expression
php regular expression

Now we come to a not very easy topic, the regular expressions. If you do not understand the subject immediately, do not worry. Even experts like to break their heads on regular expressions and for the beginning, you can live quite well without regular expressions.

What are Regular Expressions?

Regular expressions are a kind of custom programming language to search and review texts. A typical example is to check if the e-mail field is really a valid e-mail address. We check whether the input corresponds to a certain pattern. In the case of an e-mail address, if the input contains eg a @ -sign, ends with your top-level domain like .de or .com etc. In this tutorial, we give you only a very basic overview of regular expressions, much more information can be found on the Internet.

Use regular expressions in PHP

In PHP you need for regular expressions mainly the two functions preg_match ($pattern, $text) and preg_replace($pattern, $replace, $text) , With preg_match() is checked if $text some $pattern corresponds to which we have defined by regular expression. With preg_replace() we can replace certain text passages, similar to str_replace().

Let’s start small. Suppose we want to check if a text contains a number between 10 and 19. Then, of course, we can do many strpos() checks, or use a regular expression:

<?Php
$text = "We have 13 cats";
$pattern = "/1[0123456789] /";
if(preg_match($pattern, $text)) {
   echo "input contains the pattern";
} else {
   echo "input does not contain the pattern";
}
?>

We start the regular expression with a / and finish it again with a /. In between stands our pattern. We want to check if the text contains a number between 10 and 19, ie we have to make sure that the first digit is a 1 followed by a number 0 to 9. This is what we achieve in the above case, we check first if a 1 appears , followed by any number in square brackets, ie any number between 0 and 9. We can also grab several square brackets in a row and thus expand our pattern. Suppose we want to check if we have an odd number between 801 and 999 in the text, then this is as follows:

<?php
$text = "We have 133 cats";
$pattern = "/ [89] [0123456789] [13579] /";
if(preg_match($pattern, $text)) {
   echo "input contains the pattern";
} else {
   echo "input does not contain the pattern";
}
?>

We first check if there is an 8 or 9. Then we allow each number between 0 and 9. And the last number must be an odd number, ie 1, 3, 5, 7 or 9.

Instead of writing out all the characters, fortunately, we can also use areas in square brackets. Instead of [0123456789] we can also write [0-9], this has the same effect.

In addition to numbers, we can also check letters as well.

<?php
$text = "We have 133 cats";
$pattern = "/[aeiou] /"; // Check if a vowel is included in the text
if(preg_match ($pattern, $text)) {
   echo "input contains the pattern";
} else {
   echo "input does not contain the pattern";
}
?>

Overview Regular Expressions

Below is an overview of the most important commands for regular expressions.

operatorfunctionexample
.Wildcard – matches any character/h.llo/ – Fits all texts h any character followed by llo included
[]Checks if one of the characters is included/ h[ae]llo / -Fits to hello and hello
/ [A-Za-z0-9] / – Allows a capital letter, lower case or number
^Check the beginning of the text. Can not stand for./^test / – The text must be tested starting
/ hall[^aeiou] / – The letters hall must not with a, e, i, o or u end
$Check the end of the text/test$/ – The text must stop with test
|Allows alternatives/ (the|that) / -Fits to the and the
/ child (ren|ergarten | le) / – Fits childrenkindergarten and Kindle .
?Previous character is optional/ iPhone[1-7]? / -Fits iPhone , iPhone2 , etc. to iPhone7
*Repeat the previous element (0 or more times)/Windows[0-9]*/ – Fits Windows , Windows 98 and Windows 7 , but not Windows7.
+Repetition of the previous element (1 or more times)/[0-9]+/ – Fits all natural numbers.
{N}Exactly n times repetition of the previous element/[0-9]{3}/ – Fits all 3-digit numbers.
{M, n}Repetition of the previous element at least m times, a maximum of n times./[0-9]{1,4}/ – Fits all 1 to 4 digit numbers.

Typical Regular Expressions

Here are some regular expressions to demonstrate functionality.

Checking the postal code

If we want to check if an input is a (German) postcode, a possible printout would look like this: preg_match (“/ ^ [0-9] {5} $ /”input ), By the ^ and the $ we make sure that only the specified characters may occur. With [0-9] {5} we then allow all 5-digit numbers. Unfortunately, the 00000 would also be allowed here, although it is not a valid postal code.

Checking the phone number

Checking a phone number is a bit more complicated as it may include a + for the country code at the beginning. Likewise, blanks, hyphens or slashes are also used when specifying them. A possible regular expression might look like this:

preg_match “/ ^ \ +? ([0-9 \ / -] +) $ /” input ;

In order to be able to use the + character for the country code as well as the slash within a phone number, we have to escape this beforehand by means of \. So in this regular expression, we start with an optional +, and then we can do numbers, spaces, hyphens, and slashes.

Verification of the e-mail address

Checking for a valid email address is not trivial through regular expression. Luckily, PHP has the filter_var function. If you still want a regular expression for the e-mail address, the correct code would look like this:

This is terribly complicated, a simplified version would be:

Here we first check that a part exists before the @ sign, followed by the possible domain and the domain extension. Umlauts are unfortunately not possible with this simplified version.

 

About The Author

Related posts

Leave a Reply