<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Return array entries that match the pattern</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.preg-filter.html">preg_filter</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.preg-last-error.html">preg_last_error</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-grep" class="refentry"> <div class="refnamediv"> <h1 class="refname">preg_grep</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">preg_grep</span> — <span class="dc-title">Return array entries that match the pattern</span></p> </div> <div class="refsect1 description" id="refsect1-function.preg-grep-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>preg_grep</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$pattern</code></span> , <span class="methodparam"><span class="type">array</span> <code class="parameter">$input</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span> ] )</div> <p class="para rdfs-comment"> Returns the array consisting of the elements of the <em><code class="parameter">input</code></em> array that match the given <em><code class="parameter">pattern</code></em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.preg-grep-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">pattern</code></em></span> <dd> <p class="para"> The pattern to search for, as a string. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">input</code></em></span> <dd> <p class="para"> The input array. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">flags</code></em></span> <dd> <p class="para"> If set to <strong><code>PREG_GREP_INVERT</code></strong>, this function returns the elements of the input array that do <em class="emphasis">not</em> match the given <em><code class="parameter">pattern</code></em>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.preg-grep-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an array indexed using the keys from the <em><code class="parameter">input</code></em> array. </p> </div> <div class="refsect1 changelog" id="refsect1-function.preg-grep-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>4.2.0</td> <td> The <em><code class="parameter">flags</code></em> parameter was added. </td> </tr> <tr> <td>4.0.4</td> <td> <p class="para"> Prior to this version, the returned array was indexed regardless of the keys of the <em><code class="parameter">input</code></em> array. </p> <p class="para"> If you want to reproduce this old behavior, use <span class="function"><a href="function.array-values.html" class="function">array_values()</a></span> on the returned array to reindex the values. </p> </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.preg-grep-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4783"> <p><strong>Example #1 <span class="function"><strong>preg_grep()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// return all array elements<br />// containing floating point numbers<br /></span><span style="color: #0000BB">$fl_array </span><span style="color: #007700">= </span><span style="color: #0000BB">preg_grep</span><span style="color: #007700">(</span><span style="color: #DD0000">"/^(\d+)?\.\d+$/"</span><span style="color: #007700">, </span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.preg-grep-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="pcre.pattern.html" class="link">PCRE Patterns</a></li> <li class="member"> <span class="function"><a href="function.preg-match-all.html" class="function" rel="rdfs-seeAlso">preg_match_all()</a> - Perform a global regular expression match</span></li> <li class="member"> <span class="function"><a href="function.preg-filter.html" class="function" rel="rdfs-seeAlso">preg_filter()</a> - Perform a regular expression search and replace</span></li> <li class="member"> <span class="function"><a href="function.preg-last-error.html" class="function" rel="rdfs-seeAlso">preg_last_error()</a> - Returns the error code of the last PCRE regex execution</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.preg-filter.html">preg_filter</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.preg-last-error.html">preg_last_error</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></body></html>