function.preg-match.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 371 行 · 第 1/2 页
HTML
371 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Perform a regular expression match</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.preg-match-all.html">preg_match_all</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.preg-quote.html">preg_quote</a></div> <div class="up"><a href="ref.pcre.html">PCRE Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.preg-match" class="refentry"> <div class="refnamediv"> <h1 class="refname">preg_match</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">preg_match</span> — <span class="dc-title">Perform a regular expression match</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><b><b>preg_match</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$pattern</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$subject</tt></span> [, <span class="methodparam"><span class="type">array</span> <tt class="parameter reference">&$matches</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$flags</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$offset</tt></span> ]]] )</div> <p class="para rdfs-comment"> Searches <i><tt class="parameter">subject</tt></i> for a match to the regular expression given in <i><tt class="parameter">pattern</tt></i>. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">pattern</tt></i></span> <dd> <p class="para"> The pattern to search for, as a string. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">subject</tt></i></span> <dd> <p class="para"> The input string. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">matches</tt></i></span> <dd> <p class="para"> If <i><tt class="parameter">matches</tt></i> is provided, then it is filled with the results of search. <var class="varname">$matches[0]</var> will contain the text that matched the full pattern, <var class="varname">$matches[1]</var> will have the text that matched the first captured parenthesized subpattern, and so on. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">flags</tt></i></span> <dd> <p class="para"> <i><tt class="parameter">flags</tt></i> can be the following flag: <dl> <dt> <span class="term"><b><tt>PREG_OFFSET_CAPTURE</tt></b></span> <dd> <span class="simpara"> If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the return value in an array where every element is an array consisting of the matched string at index <i>0</i> and its string offset into <i><tt class="parameter">subject</tt></i> at index <i>1</i>. </span> </dd> </dt> </dl> </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">offset</tt></i></span> <dd> <p class="para"> Normally, the search starts from the beginning of the subject string. The optional parameter <i><tt class="parameter">offset</tt></i> can be used to specify the alternate place from which to start the search (in bytes). </p> <blockquote><p><b class="note">Note</b>: Using <i><tt class="parameter">offset</tt></i> is not equivalent to passing <i>substr($subject, $offset)</i> to <b>preg_match()</b> in place of the subject string, because <i><tt class="parameter">pattern</tt></i> can contain assertions such as <em class="emphasis">^</em>, <em class="emphasis">$</em> or <em class="emphasis">(?<=x)</em>. Compare: <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$subject </span><span style="color: #007700">= </span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pattern </span><span style="color: #007700">= </span><span style="color: #DD0000">'/^def/'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #0000BB">$pattern</span><span style="color: #007700">, </span><span style="color: #0000BB">$subject</span><span style="color: #007700">, </span><span style="color: #0000BB">$matches</span><span style="color: #007700">, </span><span style="color: #0000BB">PREG_OFFSET_CAPTURE</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <p class="para">The above example will output:</p> <div class="example-contents"><pre><div class="cdata"><pre>Array()</pre></div> </pre></div> <p class="para"> while this example </p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$subject </span><span style="color: #007700">= </span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pattern </span><span style="color: #007700">= </span><span style="color: #DD0000">'/^def/'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #0000BB">$pattern</span><span style="color: #007700">, </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$subject</span><span style="color: #007700">,</span><span style="color: #0000BB">3</span><span style="color: #007700">), </span><span style="color: #0000BB">$matches</span><span style="color: #007700">, </span><span style="color: #0000BB">PREG_OFFSET_CAPTURE</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <p class="para"> will produce </p> <div class="example-contents"><pre><div class="cdata"><pre>Array( [0] => Array ( [0] => def
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?