Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 5411

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!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>Perform a regular expression search and replace</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ref.pcre.html">PCRE Functions</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.preg-grep.html">preg_grep</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-filter" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">preg_filter</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0)</p><p class="refpurpose"><span class="refname">preg_filter</span> &mdash; <span class="dc-title">Perform a regular expression search and replace</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.preg-filter-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>preg_filter</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$pattern</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$replacement</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$subject</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$limit</code><span class="initializer"> = -1</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$count</code></span>
  ]] )</div>

  <p class="para rdfs-comment">
    <span class="function"><strong>preg_filter()</strong></span> is identical to  <span class="function"><a href="function.preg-replace.html" class="function">preg_replace()</a></span>
   except it only returns the (possibly transformed) subjects where there was
   a match. For details about how this function works, read the
    <span class="function"><a href="function.preg-replace.html" class="function">preg_replace()</a></span> documentation.
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.preg-filter-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns an <span class="type"><a href="language.types.array.html" class="type array">array</a></span> if the <em><code class="parameter">subject</code></em>
   parameter is an <span class="type"><a href="language.types.array.html" class="type array">array</a></span>, or a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> otherwise.
  </p>
  <p class="para">
   If no matches are found or an error occurred, an empty <span class="type"><a href="language.types.array.html" class="type array">array</a></span> 
   is returned when <em><code class="parameter">subject</code></em> is an <span class="type"><a href="language.types.array.html" class="type array">array</a></span>
   or <strong><code>NULL</code></strong> otherwise.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.preg-filter-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-4782">
    <p><strong>Example #1 
     Example comparing  <span class="function"><strong>preg_filter()</strong></span> 
     with  <span class="function"><a href="function.preg-replace.html" class="function">preg_replace()</a></span>
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$subject&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'2'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'3'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'A'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'B'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'4'</span><span style="color: #007700">);&nbsp;<br /></span><span style="color: #0000BB">$pattern&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'/\d/'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'/[a-z]/'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'/[1a]/'</span><span style="color: #007700">);&nbsp;<br /></span><span style="color: #0000BB">$replace&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'A:$0'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'B:$0'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'C:$0'</span><span style="color: #007700">);&nbsp;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"preg_filter&nbsp;returns\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">preg_filter</span><span style="color: #007700">(</span><span style="color: #0000BB">$pattern</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$replace</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$subject</span><span style="color: #007700">));&nbsp;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"preg_replace&nbsp;returns\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">preg_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$pattern</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$replace</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$subject</span><span style="color: #007700">));&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
preg_filter returns
Array
(
    [0] =&gt; A:C:1
    [1] =&gt; B:C:a
    [2] =&gt; A:2
    [3] =&gt; B:b
    [4] =&gt; A:3
    [7] =&gt; A:4
)
preg_replace returns
Array
(
    [0] =&gt; A:C:1
    [1] =&gt; B:C:a
    [2] =&gt; A:2
    [3] =&gt; B:b
    [4] =&gt; A:3
    [5] =&gt; A
    [6] =&gt; B
    [7] =&gt; A:4
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.preg-filter-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-replace.html" class="function" rel="rdfs-seeAlso">preg_replace()</a> - Perform a regular expression search and replace</span></li>
    <li class="member"> <span class="function"><a href="function.preg-replace-callback.html" class="function" rel="rdfs-seeAlso">preg_replace_callback()</a> - Perform a regular expression search and replace using a callback</span></li>
    <li class="member"> <span class="function"><a href="function.preg-grep.html" class="function" rel="rdfs-seeAlso">preg_grep()</a> - Return array entries that match the pattern</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="ref.pcre.html">PCRE Functions</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.preg-grep.html">preg_grep</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>