<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.7 $ -->
<!-- splitted from ./en/functions/pcre.xml, last change in rev 1.47 -->
  <refentry id="function.preg-replace-callback">
   <refnamediv>
    <refname>preg_replace_callback</refname>
    <refpurpose>Perform a regular expression search and replace using a callback</refpurpose>
   </refnamediv>
   <refsect1>
    <title>Description</title>
    <methodsynopsis>
     <type>mixed</type><methodname>preg_replace_callback</methodname>
     <methodparam><type>mixed</type><parameter>pattern</parameter></methodparam>
     <methodparam><type>callback</type><parameter>callback</parameter></methodparam>
     <methodparam><type>mixed</type><parameter>subject</parameter></methodparam>
     <methodparam choice="opt"><type>int</type><parameter>limit</parameter></methodparam>
    </methodsynopsis>
    <para>
     The behavior of this function is almost identical to
     <function>preg_replace</function>, except for the fact that instead of
     <parameter>replacement</parameter> parameter, one should specify a
     <parameter>callback</parameter> that will be called and passed an array of
     matched elements in the subject string. The callback should return the
     replacement string.
    </para>
    <example>
     <title><function>preg_replace_callback</function> example</title>
     <programlisting role='php'>
<![CDATA[
<?php
  // this text was used in 2002
  // we want to get this up to date for 2003
  $text = "April fools day is 04/01/2002\n";
  $text.= "Last christmas was 12/24/2001\n";

  // the callback function
  function next_year($matches) {
    // as usual: $matches[0] is the complete match
    // $matches[1] the match for the first subpattern
    // enclosed in '(...)' and so on
    return $matches[1].($matches[2]+1);
  }

  echo preg_replace_callback(
              "|(\d{2}/\d{2}/)(\d{4})|",
              "next_year",
              $text);

  // result is:
  // April fools day is 04/01/2003
  // Last christmas was 12/24/2002
?>
]]>
     </programlisting>
    </example>
    <para>
     You'll often need the <parameter>callback</parameter> function
     for a <function>preg_replace_callback</function> in just one place.
     In this case you can use <function>create_function</function> to
     declare an anonymous function as callback within the call to
     <function>preg_replace_callback</function>. By doing it this way
     you have all information for the call in one place and do not
     clutter the function namespace with a callback functions name
     not used anywhere else.
    </para>
    <example>
     <title><function>preg_replace_callback</function> and <function>create_function</function></title>
     <programlisting role='php'>
<![CDATA[
<?php
  /* a unix-style command line filter to convert uppercase
   * letters at the beginning of paragraphs to lowercase */

  $fp = fopen("php://stdin", "r") or die("can't read stdin");
  while (!feof($fp)) {
      $line = fgets($fp);
      $line = preg_replace_callback(
          '|<p>\s*\w|',
          create_function(
              // single quotes are essential here,
              // or alternative escape all $ as \$
              '$matches',
              'return strtolower($matches[0]);'
          ),
          $line
      );
      echo $line;
  }
  fclose($fp);
?>
]]>
     </programlisting>
    </example>
    <para>
     See also <function>preg_replace</function> and
     <function>create_function</function>.
    </para>
   </refsect1>
  </refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->