{"id":2700,"date":"2021-09-18T07:30:16","date_gmt":"2021-09-18T07:30:16","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=2700"},"modified":"2021-09-18T08:34:36","modified_gmt":"2021-09-18T08:34:36","slug":"php-preg_match","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-preg_match\/","title":{"rendered":"PHP preg_match"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the PHP <code>preg_match()<\/code> function to match a regular expression.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-preg_match-function'>Introduction to the PHP preg_match() function <a href=\"#introduction-to-the-php-preg_match-function\" class=\"anchor\" id=\"introduction-to-the-php-preg_match-function\" title=\"Anchor for Introduction to the PHP preg_match() function\">#<\/a><\/h2>\n\n\n\n<p>The <code>preg_match()<\/code> finds the string for a match to a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-regular-expressions\/\">regular expression<\/a>. The <code>preg_match()<\/code> function stops searching as long as it finds the first match. <\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>preg_match()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">preg_match(\n    string $pattern,\n    string $subject,\n    <span class=\"hljs-keyword\">array<\/span> &amp;$matches = <span class=\"hljs-keyword\">null<\/span>,\n    int $flags = <span class=\"hljs-number\">0<\/span>,\n    int $offset = <span class=\"hljs-number\">0<\/span>\n): int|<span class=\"hljs-keyword\">false<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>preg_match()<\/code> function accepts the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>$pattern<\/code> a string that specifies a pattern to search.<\/li><li><code>$subject<\/code> is an input string.<\/li><li><code>$matches<\/code> is an array that stores the search results. The <code>$matches<\/code> array is optional. If there is a match, the <code>$matches[0]<\/code> will contain the text that matches the whole pattern, <code>$matches[1]<\/code> will contain the text that matches the first capturing group, and so on.<\/li><li><code>$flags<\/code> is a combination of the following flags: <code>PREG_OFFSET_CAPTURE<\/code> and <code>PREG_UNMATCHED_AS_NULL<\/code>. More on these flags in the example below.<\/li><li><code>$offset<\/code> is the position that the function will start searching. By default, the <code>preg_match()<\/code> starts searching from the beginning of the string. Note that the <code>$offset<\/code> is in bytes.<\/li><\/ul>\n\n\n\n<p>The <code>preg_match()<\/code> function returns <code>1<\/code> if it finds a match,<code>0<\/code> if it doesn&#8217;t, or <code>false<\/code> on failure.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-preg_match-function-examples'>PHP preg_match() function examples <a href=\"#php-preg_match-function-examples\" class=\"anchor\" id=\"php-preg_match-function-examples\" title=\"Anchor for PHP preg_match() function examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the <code>preg_match()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-using-the-php-preg_match-to-match-a-number'>1) Using the PHP preg_match() to match a number <a href=\"#1-using-the-php-preg_match-to-match-a-number\" class=\"anchor\" id=\"1-using-the-php-preg_match-to-match-a-number\" title=\"Anchor for 1) Using the PHP preg_match() to match a number\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>preg_match()<\/code> to match a number with one or more digits using the <code>\\d+<\/code> <a href=\"https:\/\/phptutorial.net\/php-tutorial\/regex-character-classes\/\">character class<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pattern = <span class=\"hljs-string\">'\/\\d+\/'<\/span>;\n$str = <span class=\"hljs-string\">'PHP first released in 8 June 1995'<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> (preg_match($pattern, $str, $matches)) {\n    print_r($matches);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;<span class=\"hljs-number\">0<\/span>] =&gt; <span class=\"hljs-number\">8<\/span>\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the <code>preg_match()<\/code> stops searching as soon as it finds a match. In this example, 1995 also matches the pattern <code>\\d+<\/code>. However, the <code>preg_match()<\/code> already finds a match with the number 8.<\/p>\n\n\n\n<p>To find all the matches, you need to use the <code>preg_match_all()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='2-using-the-php-preg_match-to-match-a-word-character'>2) Using the PHP preg_match() to match a word character <a href=\"#2-using-the-php-preg_match-to-match-a-word-character\" class=\"anchor\" id=\"2-using-the-php-preg_match-to-match-a-word-character\" title=\"Anchor for 2) Using the PHP preg_match() to match a word character\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>preg_match()<\/code> function to match one or more word characters:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pattern = <span class=\"hljs-string\">'\/\\w+\/'<\/span>;\n$str = <span class=\"hljs-string\">'PHP first released in 8 June 1995'<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> (preg_match($pattern, $str, $matches)) {\n    print_r($matches);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;<span class=\"hljs-number\">0<\/span>] =&gt; PHP\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='3-using-the-php-preg_match-with-a-capturing-group-example'>3) Using the PHP preg_match() with a capturing group example <a href=\"#3-using-the-php-preg_match-with-a-capturing-group-example\" class=\"anchor\" id=\"3-using-the-php-preg_match-with-a-capturing-group-example\" title=\"Anchor for 3) Using the PHP preg_match() with a capturing group example\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>preg_match()<\/code> function to match a number that starts with <code>19<\/code> and is followed by exactly two digits. The pattern also has a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/regex-capturing-groups\/\">capturing group<\/a> that captures the last two digits:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pattern = <span class=\"hljs-string\">'\/19(\\d{2})\/'<\/span>;\n$str = <span class=\"hljs-string\">'PHP first released in 8 June 1995'<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> (preg_match($pattern, $str, $matches)) {\n    print_r($matches);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;<span class=\"hljs-number\">0<\/span>] =&gt; <span class=\"hljs-number\">1995<\/span>\n    &#91;<span class=\"hljs-number\">1<\/span>] =&gt; <span class=\"hljs-number\">95<\/span>\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>$matches<\/code> array contains two elements. The first element contains the text that matches the whole pattern, while the second element contains the first capturing group <code>(\\d{2})<\/code>.<\/p>\n\n\n\n<p>The following example uses the same pattern with the additional named capturing group:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pattern = <span class=\"hljs-string\">'\/19(?&lt;year&gt;\\d{2})\/'<\/span>;\n$str = <span class=\"hljs-string\">'PHP first released in 8 June 1995'<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> (preg_match($pattern, $str, $matches)) {\n    print_r($matches);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">(\n    &#91;<span class=\"hljs-number\">0<\/span>] =&gt; <span class=\"hljs-number\">1995<\/span>\n    &#91;year] =&gt; <span class=\"hljs-number\">95<\/span>\n    &#91;<span class=\"hljs-number\">1<\/span>] =&gt; <span class=\"hljs-number\">95<\/span>\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='4-using-the-php-preg_match-function-with-the-preg_offset_capture-flag'>4) Using the PHP preg_match() function with the PREG_OFFSET_CAPTURE flag <a href=\"#4-using-the-php-preg_match-function-with-the-preg_offset_capture-flag\" class=\"anchor\" id=\"4-using-the-php-preg_match-function-with-the-preg_offset_capture-flag\" title=\"Anchor for 4) Using the PHP preg_match() function with the PREG_OFFSET_CAPTURE flag\">#<\/a><\/h3>\n\n\n\n<p>Sometimes, you want to find a match in a string and the starting position of the match. To do that, you use the <code>PREG_OFFSET_CAPTURE<\/code> flag. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pattern = <span class=\"hljs-string\">'\/&#91;A-Z]{3}\/'<\/span>;\n$str = <span class=\"hljs-string\">'Hello PHP'<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> (preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE)) {\n    print_r($matches);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;<span class=\"hljs-number\">0<\/span>] =&gt; <span class=\"hljs-keyword\">Array<\/span>\n        (\n            &#91;<span class=\"hljs-number\">0<\/span>] =&gt; PHP\n            &#91;<span class=\"hljs-number\">1<\/span>] =&gt; <span class=\"hljs-number\">6<\/span>\n        )\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>[A-Z]{3}<\/code> pattern matches any text with three letters in uppercase. Since we use the <code>PREG_OFFSET_CAPTURE<\/code> flag, it returns the match (PHP) and the starting position (or offset) of the text PHP in the string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='5-using-the-preg_match-function-with-the-preg_unmatched_as_null-flag'>5) Using the preg_match() function with the PREG_UNMATCHED_AS_NULL flag <a href=\"#5-using-the-preg_match-function-with-the-preg_unmatched_as_null-flag\" class=\"anchor\" id=\"5-using-the-preg_match-function-with-the-preg_unmatched_as_null-flag\" title=\"Anchor for 5) Using the preg_match() function with the PREG_UNMATCHED_AS_NULL flag\">#<\/a><\/h3>\n\n\n\n<p>By default, the <code>preg_match()<\/code> function returns an empty string for the unmatched capturing group (or subpattern).<\/p>\n\n\n\n<p>If you want the <code>preg_match()<\/code> function to return null instead, you can use the <code>PREG_UNMATCHED_AS_NULL<\/code> flag.<\/p>\n\n\n\n<p>The following example uses the <code>preg_match()<\/code> function without the <code>PREG_UNMATCHED_AS_NULL<\/code> flag.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pattern = <span class=\"hljs-string\">'\/&#91;A-Z]{3}(\\s*)(\\d+.\\d*.\\d*)*\/'<\/span>;\n$str = <span class=\"hljs-string\">'Hello PHP 8.0.1'<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> (preg_match($pattern, $str, $matches)) {\n    var_dump($matches);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">array<\/span>(<span class=\"hljs-number\">3<\/span>) {\n    &#91;<span class=\"hljs-number\">0<\/span>]=&gt;  string(<span class=\"hljs-number\">9<\/span>) <span class=\"hljs-string\">\"PHP 8.0.1\"<\/span>\n    &#91;<span class=\"hljs-number\">1<\/span>]=&gt;  string(<span class=\"hljs-number\">1<\/span>) <span class=\"hljs-string\">\" \"<\/span>\n    &#91;<span class=\"hljs-number\">2<\/span>]=&gt;  string(<span class=\"hljs-number\">5<\/span>) <span class=\"hljs-string\">\"8.0.1\"<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the string has the text that matches the subpatterns <code>(\\s*)(\\d+.\\d*.\\d\\*)<\/code>.<\/p>\n\n\n\n<p>However, in the following example, the string doesn&#8217;t have a text that matches the subpattern:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pattern = <span class=\"hljs-string\">'\/&#91;A-Z]{3}(\\s*)(\\d+.\\d*.\\d*)*\/'<\/span>;\n$str = <span class=\"hljs-string\">'Hello PHP'<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> (preg_match($pattern, $str, $matches)) {\n    var_dump($matches);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">array<\/span>(<span class=\"hljs-number\">2<\/span>) {\n    &#91;<span class=\"hljs-number\">0<\/span>]=&gt;  string(<span class=\"hljs-number\">3<\/span>) <span class=\"hljs-string\">\"PHP\"<\/span>\n    &#91;<span class=\"hljs-number\">1<\/span>]=&gt;  string(<span class=\"hljs-number\">0<\/span>) <span class=\"hljs-string\">\"\"<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The string doesn&#8217;t have any text that matches the <code>(\\s\\*)<\/code> subpattern, the <code>preg_match()<\/code> returns an empty string.<\/p>\n\n\n\n<p>Since we use the <code>PREG_UNMATCHED_AS_NULL<\/code> flag, the <code>preg_match()<\/code> function returns null for the unmatched subpattern instead.<\/p>\n\n\n\n<p>The following example uses the <code>PREG_UNMATCHED_AS_NULL<\/code> flag. So it returns <code>NULL<\/code> instead of an empty string for the unmatched subpattern:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pattern = <span class=\"hljs-string\">'\/&#91;A-Z]{3}(\\s*)(\\d+.\\d*.\\d*)*\/'<\/span>;\n$str = <span class=\"hljs-string\">'Hello PHP'<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> (preg_match($pattern, $str, $matches, PREG_UNMATCHED_AS_NULL)) {\n    var_dump($matches);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">array<\/span>(<span class=\"hljs-number\">2<\/span>) {\n    &#91;<span class=\"hljs-number\">0<\/span>]=&gt;  string(<span class=\"hljs-number\">3<\/span>) <span class=\"hljs-string\">\"PHP\"<\/span>\n    &#91;<span class=\"hljs-number\">1<\/span>]=&gt;  <span class=\"hljs-keyword\">NULL<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='6-using-the-php-preg_match-with-the-offset-parameter'>6) Using the php preg_match() with the offset parameter <a href=\"#6-using-the-php-preg_match-with-the-offset-parameter\" class=\"anchor\" id=\"6-using-the-php-preg_match-with-the-offset-parameter\" title=\"Anchor for 6) Using the php preg_match() with the offset parameter\">#<\/a><\/h3>\n\n\n\n<p>Suppose that you have the following string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">PHP <span class=\"hljs-number\">1.0<\/span> released in <span class=\"hljs-number\">1995<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you use the <code>\\d+<\/code>, it&#8217;ll match the first number, which is 1.<\/p>\n\n\n\n<p>However, if you pass an offset parameter that specifies the starting position to search, it&#8217;ll match the second number <code>1995<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pattern = <span class=\"hljs-string\">'\/\\d+\/'<\/span>;\n$str = <span class=\"hljs-string\">' PHP 1.0 released in 1995'<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span>(preg_match($pattern, $str,$matches, PREG_OFFSET_CAPTURE, <span class=\"hljs-number\">10<\/span>)) {\n    print_r($matches);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;<span class=\"hljs-number\">0<\/span>] =&gt; <span class=\"hljs-keyword\">Array<\/span>\n        (\n            &#91;<span class=\"hljs-number\">0<\/span>] =&gt; <span class=\"hljs-number\">1995<\/span>\n            &#91;<span class=\"hljs-number\">1<\/span>] =&gt; <span class=\"hljs-number\">21<\/span>\n        )\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>preg_match()<\/code> function starts searching for matches from position ten instead of 0.<\/p>\n\n\n\n<p>Notice that the pattern may contain assertions like <code>^<\/code>, <code>$<\/code>, or <code>(?&lt;=A)<\/code>. Therefore, the offset is not equivalent to passing the substring to the <code>preg_match()<\/code> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the PHP <code>preg_match()<\/code> function to search for a match with a pattern in a string.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2700\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-preg_match\/\"\n\t\t\t\tdata-post-title=\"PHP preg_match\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2700\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-preg_match\/\"\n\t\t\t\tdata-post-title=\"PHP preg_match\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the PHP preg_match() function to match a regular expression.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":138,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2700","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=2700"}],"version-history":[{"count":4,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2700\/revisions"}],"predecessor-version":[{"id":2706,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2700\/revisions\/2706"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=2700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}