cvs: phpdoc /en/reference/pcre/functions preg-replace-callback.xml

From: Date: Thu, 20 Feb 2003 10:27:21 +0000
Subject: cvs: phpdoc /en/reference/pcre/functions preg-replace-callback.xml
Groups: php.doc 
Request: Send a blank email to phpdoc+get-969351869@lists.php.net to get a copy of this message
hholzgra Thu Feb 20 05:27:21 2003 EDT Modified files: /phpdoc/en/reference/pcre/functions preg-replace-callback.xml Log: some functions really desere examples Index: phpdoc/en/reference/pcre/functions/preg-replace-callback.xml diff -u phpdoc/en/reference/pcre/functions/preg-replace-callback.xml:1.3 phpdoc/en/reference/pcre/functions/preg-replace-callback.xml:1.4 --- phpdoc/en/reference/pcre/functions/preg-replace-callback.xml:1.3 Wed Dec 11 07:51:23 2002 +++ phpdoc/en/reference/pcre/functions/preg-replace-callback.xml Thu Feb 20 05:27:20 2003 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.3 $ --> +<!-- $Revision: 1.4 $ --> <!-- splitted from ./en/functions/pcre.xml, last change in rev 1.47 --> <refentry id="function.preg-replace-callback"> <refnamediv> @@ -23,8 +23,76 @@ matched elements in the subject string. The callback should return the replacement string. </para> + <example> + <title><function>preg_replace_callback</function></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>. + See also <function>preg_replace</function>, <function>create_function</function>. </para> </refsect1> </refentry>

« previous php.doc (#969351869) next »