# preg_match

Perform a regular expression match

## Syntax

`preg_match ( string \$pattern , string \$subject [, array &\$matches [, int \$flags = 0 [, int \$offset = 0 ]]] ) : int`

### Parameters

`pattern`

The pattern to search for, as a string.

`subject`

The input string.

`matches`

If matches is provided, then it is filled with the results of search. \$matches will contain the text that matched the full pattern, \$matches will have the text that matched the first captured parenthesized subpattern, and so on.

`flags`

flags can be a combination of the following flags:

 PREG_OFFSET_CAPTURE For every occurring match the appendant string offset (in bytes) 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 into subject at offset 1. PREG_UNMATCHED_AS_NULL Unmatched subpatterns are reported as NULL; otherwise they are reported as an empty string.
`offset`

Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes).

Note: Using offset is not equivalent to passing substr(\$subject, \$offset) to preg_match() in place of the subject string, because pattern can contain assertions such as ^, \$ or (?<=x). Alternatively, to avoid using substr(), use the \G assertion rather than the ^ anchor, or the A modifier instead, both of which work with the offset parameter.

### Return

preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

Warning: This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the identity operator (===) for testing the return value of this function.

## Examples

### 1 · pattern subject · Case-Sensitive

```<?

\$pattern = "#php#";
\$subject = "PHP stands for PHP: Hypertext Preprocessor";

\$return = preg_match(\$pattern, \$subject);

echo \$return;

?>```
`0`

### 2 · pattern subject · Case-Insensitive

```<?

\$pattern = "#php#i";
\$subject = "PHP stands for PHP: Hypertext Preprocessor";

\$return = preg_match(\$pattern, \$subject);

echo \$return;

?>```
`1`

### 3 · matches

```<?

\$pattern = "#php#i";
\$subject = "PHP stands for PHP: Hypertext Preprocessor";

preg_match(\$pattern, \$subject, \$matches);

print_r(\$matches);

?>```
```Array
(
 => PHP
)
```

### 4 · flags · PREG_OFFSET_CAPTURE

```<?

\$pattern = "#(preg)(offset)(capture)#";
\$subject = "pregoffsetcapture";
\$flags = PREG_OFFSET_CAPTURE;

preg_match(\$pattern, \$subject, \$matches, \$flags);

print_r(\$matches);

?>```
```Array
(
 => Array
(
 => pregoffsetcapture
 => 0
)

 => Array
(
 => preg
 => 0
)

 => Array
(
 => offset
 => 4
)

 => Array
(
 => capture
 => 10
)

)
```

### 5 · flags · PREG_UNMATCHED_AS_NULL

```<?

\$pattern = "#(preg)(unmatched)*(as)#";
\$subject = "pregas";
\$flags = PREG_UNMATCHED_AS_NULL;

preg_match(\$pattern, \$subject, \$matches1);
preg_match(\$pattern, \$subject, \$matches2, \$flags);

var_export(\$matches1);
echo PHP_EOL;
var_export(\$matches2);

?>```
```array (
0 => 'pregas',
1 => 'preg',
2 => '',
3 => 'as',
)
array (
0 => 'pregas',
1 => 'preg',
2 => NULL,
3 => 'as',
)```

### 6 · offset

```<?

\$pattern = "#offset#";
\$subject = "offset offset offset";
\$flags = PREG_OFFSET_CAPTURE;
\$offset = 1;

preg_match(\$pattern, \$subject, \$matches, \$flags, \$offset);

print_r(\$matches);

?>```
```Array
(
 => Array
(
 => offset
 => 7
)

)
```

### 7 · Word Boundary

```<?

\$pattern = "#\bweb\b#";
\$subject1 = "PHP is the web scripting language of choice.";
\$subject2 = "PHP is the website scripting language of choice.";

\$return1 = preg_match(\$pattern, \$subject1);
\$return2 = preg_match(\$pattern, \$subject2);

if (\$return1) {
echo "found\n";
} else {
}
if (\$return2) {
echo "found";
} else {
}

?>```
```found

### 8 · Named Subpattern

```<?

\$pattern = "#(?<name>\w+): (?<digit>\d+)#";
\$subject = "year: 2001";

preg_match(\$pattern, \$subject, \$matches);

print_r(\$matches);

?>```
```Array
(
 => year: 2001
[name] => year
 => year
[digit] => 2001
 => 2001
)
```

### 9 · URL

```<?

\$pattern = "#^(?:https?://)?([^/]+)#i";
\$subject = "https://www.php.net/index.html";

preg_match(\$pattern, \$subject, \$matches);

\$pattern = "#[^.]+\.[^.]+\$#";

preg_match(\$pattern, \$matches, \$matches);

echo "second-level and top-level domain: \$matches";

?>```
`second-level and top-level domain: php.net`
{"ft_lang_label":"__label__en","ft_lang_prob":0.62436557,"math_prob":0.8428901,"size":4398,"snap":"2023-40-2023-50","text_gpt3_token_len":1215,"char_repetition_ratio":0.19458352,"word_repetition_ratio":0.11964549,"special_character_ratio":0.33492497,"punctuation_ratio":0.21681997,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95417076,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-22T05:09:06Z\",\"WARC-Record-ID\":\"<urn:uuid:68f41f4b-97f2-4b11-8996-1bca2415e6af>\",\"Content-Length\":\"164073\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5eba0240-d0e9-4e92-8a48-6539e5b0660d>\",\"WARC-Concurrent-To\":\"<urn:uuid:a94dea24-54c6-428e-a664-71293f0a8193>\",\"WARC-IP-Address\":\"212.1.210.80\",\"WARC-Target-URI\":\"https://osbo.com/php/pcre/preg_match/\",\"WARC-Payload-Digest\":\"sha1:UDX72KFA5LD5YZ3JNHC47FHZYJDW3BO4\",\"WARC-Block-Digest\":\"sha1:IZJONMILIDXA4G7DDFLGZYVF46S7FAFF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506329.15_warc_CC-MAIN-20230922034112-20230922064112-00454.warc.gz\"}"}