Blog de programación, errores, soluciones

Chose Language:
comments
Author: Admin/Publisher |finished | checked

PHP preg_match funtion

The PHP function preg_match compares a given string $subject with a given regular expression $pattern.

Description / Descripción
preg_match(
string $pattern,
string $subject,
array &$matches = null,
int $flags = 0,
int $offset = 0
): int|false

Parameters

$pattern – the pattern of the regular expression that will be compared with the given string.

$subject – is the string with which we will compare the regular expression.

$matches – an array that we will provide if we want to obtain matches of patterns in $pattern compared to the string.

$matches[0] will contain the text that matched the complete pattern given in $pattern.

$matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

$flags – we can pass the following flags as parameters:

First, let’s consider the case of having no flags: The flag value is 0 by default.

preg_match('/(foo)(bar)(baz)/', 'foobarbaz', $matches);
print_r($matches);
Array
(
    [0] => foobarbaz
    [1] => foo
    [2] => bar
    [3] => baz
)

PREG_OFFSET_CAPTURE

If this constant is used as a flag parameter, the index where the match was found will also be returned in $matches.

preg_match('/(foo)(bar)(baz)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
Array
(
    [0] => Array
        (
            [0] => foobarbaz
            [1] => 0
        )

    [1] => Array
        (
            [0] => foo
            [1] => 0
        )

    [2] => Array
        (
            [0] => bar
            [1] => 3
        )

    [3] => Array
        (
            [0] => baz
            [1] => 6
        )
)

PREG_UNMATCHED_AS_NULL

When we pass this constant as a flag, non-matching sub-patterns will be reported as null in $matches; if not used, they will be reported as an empty string. ""

<?php
preg_match('/(a)(b)*(c)/', 'ac', $matches);
var_dump($matches);
preg_match('/(a)(b)*(c)/', 'ac', $matches, PREG_UNMATCHED_AS_NULL);
var_dump($matches);
?>
array(4) {
  [0]=>
  string(2) "ac"
  [1]=>
  string(1) "a"
  [2]=>
  string(0) ""
  [3]=>
  string(1) "c"
}
array(4) {
  [0]=>
  string(2) "ac"
  [1]=>
  string(1) "a"
  [2]=>
  NULL
  [3]=>
  string(1) "c"
}

$offset – This parameter is optional and can be used to specify the starting point for the search for matches (in bytes).

Returns

1 – If a pattern matching the given pattern in $pattern is found in the string.

0 – If no matches are found with the pattern $pattern.

false in case of a failure.

This function can return false and may also return non-boolean values that can be evaluated as false, such as 0 and “”, therefore, the use of the === operator is recommended.

This time we won’t see examples of the function, as there are already enough examples in its documentation. However, we will look at its use cases.

This function can be used in form validation, data extraction, and keyword searches. Its primary use, though, is in form validation and password validation.

We will delve deeper when writing about Form Validation in PHP.

Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category

Comments