From the author: Greetings, friends. In this article, we will directly begin to study the capabilities of regular expressions, get acquainted with the preg_match() function in PHP, and also create the first regular expression to check for a number.

It’s probably worth starting this article with the fact that regular expressions are a very powerful tool for working with text. As you already know, with the help of regular expressions we can find literally anything in an array of text. However, such power should not be abused. Don't forget that PHP offers us a lot of functions for working with strings, types, and so on, which allow us to check, say, the presence of a certain character or substring in a string.

Therefore, if it is possible to solve a problem without using regular expressions, then it is worth using this opportunity, since functions for working with regular expressions are usually more resource-intensive. In this case, the problem can be solved without regular expressions. For example, you can check whether all characters in a string are numeric using the ctype_digit() function.

But according to the conditions of the task posed in the article, we need to use regular expressions, so we will solve the task using the preg_match() function. This function checks a string against a regular expression pattern. The function has 5 parameters, of which in practice only the first 2 are almost always used.

The first parameter of the function will be the regular expression pattern. The second is the string being checked. Sometimes we may need a third parameter, specifying which we will get an array with search results.

Let's create our first template. It may look like this:

$pattern = "#^+$#";

$pattern = "#^+$#" ;

At first glance it may look incomprehensible and somewhat scary. However, there is nothing terrible here, now we will look at it. Let's start with the fact that regular expressions actively use special characters, the so-called. metacharacters. There are quite a lot of them in our template:

^ — metacharacter for the beginning of a line

$ - end-of-line metacharacter

- character class metacharacters

+ is a quantifier that denotes 1 or more occurrences of a character or group of characters followed by a quantifier.

So, what did we get? How can you read a composed regular expression pattern? And it reads like this:

the entire string will be validated, from beginning to end (metacharacters ^$)

the line can only contain numbers from 0 to 9 ()

the line must contain at least one digit (+)

Now let's check the template in action.

As you can see, the regular expression works correctly. Only lines consisting entirely of numbers fit under it. By the way, if you are also testing on the site regexr.com and there is more than one line in the text, then you need to specify a special flag for the template - m, which will allow the template to work with multiline text. You can do this in the upper right corner, menu item flags.

Note what happens if we remove the start and/or end of line metacharacters:

Now we are not validating the entire line, but simply looking for something in the line. As a result, numbers were found in the third line, although according to the conditions of the problem this line does not suit us. Now let's use the preg_match function and check each of the lines individually:

$arr = ["123", "test", "45ew45", "456"]; $pattern = "#^+$#"; foreach($arr as $item)( if(preg_match($pattern, $item))( echo "

The $item string contains only numbers

"; )else( echo "

The $item line is not suitable

"; } }

$arr = [ "123" , "test" , "45ew45" , "456" ] ;

$pattern = "#^+$#" ;

foreach ($arr as $item) (

For example, take the following address: http://example.com/price.php?product=859844&page=99.

The script displays a list of prices in stores for the product product; the optional page parameter specifies the page number. If page is not specified and the url looks like http://example.com/price.php?product=859844 , then we display the first page.

Before PHP versions 5.2.0 the problem could be solved in such a simple way.

// Function for getting a parameter that is natural number// $arr = array of parameters ($GET or $POST), $name = parameter name, // The function returns the value of the parameter, or $default if the parameter is missing or incorrect function get_param_nat($arr, $name, $default=null ) ( if (!isset($arr[$name])) return $default; // Check in a very simple way, convert the parameter to a number, then back to a string // If everything is fine, then the resulting string should match the original value of the parameter $val = $arr[$name]; $intval = intval($val); // For the load, check that the number we have is greater than zero if (strval($intval) !== $val || $intval< 1) return $default; return $intval; } // Проверяем параметр product if (($product = get_param_nat($_GET, "product")) === null) die("Product not found"); // Get the page number $page = get_param_nat($_GET, "page", 1);

Starting in version 5.2.0, a group of Filter functions appeared in PHP: filter_var, filter_input, filter_var_array and several others. Functions can check variables for integers, floating point numbers, e‑mail, ip‑addresses, url, etc., and also clear strings according to specified parameters.

Let's rewrite the code using the filter_input function.

Function get_param_nat($type, $name, $default=null) ( $val = filter_input($type, FILTER_VALIDATE_INT, array("min_range" => 1, "max_range" => PHP_INT_MAX)); // filter_input returns false if filtering failed, or null if the variable is undefined if ($val === null || $val === false) return $default; return $val; ) // Check the product parameter if (($product = get_param_nat(INPUT_GET , "product")) === null) die("Product not found"); // Get the page number $page = get_param_nat(INPUT_GET, "page", 1);

A little testing showed that the speed of operation of the two variants of the get_param_nat function is almost the same, and in the absence of a parameter, for example, page is not specified, the first variant will work even somewhat faster.

So it's up to you to decide what to use. I personally use the first function; it is somehow closer and clearer to me.

Well, a small lyrical digression at the end. To avoid duplicating the page for search engines, for example, without page and with page=1, I recommend specifying the canonical url in the head section of the page. It will look something like this:

...