Php Preg_Match
# PHP preg_match() Function
[PHP Regular Expressions (PCRE)](#)
The preg_match function is used to perform a regular expression match.
### Syntax
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
Searches subject for a match to the regular expression given in pattern.
Parameter Description:
* $pattern: The pattern to search for, as a string.
* $subject: The input string.
* $matches: If matches is provided, it will be populated with the results of the search. $matches will contain the text that matched the full pattern, $matches will contain the text that matched the first captured subpattern, and so on.
* $flags: flags can be set to the following mark values:
1. PREG_OFFSET_CAPTURE: If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset in subject at offset 1.
* offset: Normally, the search starts from the beginning of the subject string. The optional offset parameter is used to specify the position in the subject to start searching (in bytes).
### Return Value
Returns the number of full matches. Its value will be 0 (no match) or 1, because preg_match() will stop searching after the first match. preg_match_all() is different in that it will search the subject until it reaches the end of the string. Returns FALSE on error.
### Examples
## Find the text string "php":
```php
The result of the execution is as follows:
A match was found: php.
* * *
## Find the word "web"
```php
The result of the execution is as follows:
A match was found.
A match was not found.
* * *
## Get the domain name from a URL
```php
The result of the execution is as follows:
domain name is:
* * *
## Using named subgroups
```php
<?php
$str = 'foobar: 2008';
preg_match('/(?Pw+): (?Pd+)/', $str, $matches);
/* The following example works in php 5.2.2 (pcre 7.0) or later,
however, for backward compatibility, the above way is recommended. */
// preg_match('/(?w+): (?d+)/', $str, $matches);
print_r($matches);
?>
The result of the execution is as follows:
Array
(
=> foobar: 2008
=> foobar
=> foobar
=> 2008
=> 2008
)
[PHP Regular Expressions (PCRE)](#)
YouTip