<!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, PHP 7)</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 <code class="parameter">input</code> array that match the given <code class="parameter">pattern</code>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.preg-grep-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">pattern</code></dt> <dd> <p class="para"> The pattern to search for, as a string. </p> </dd> <dt> <code class="parameter">input</code></dt> <dd> <p class="para"> The input array. </p> </dd> <dt> <code class="parameter">flags</code></dt> <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 <code class="parameter">pattern</code>. </p> </dd> </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 <code class="parameter">input</code> array. </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-5984"> <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-quote.html" class="function" rel="rdfs-seeAlso">preg_quote()</a> - Quote regular expression characters</span></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>