Bug # 21816 Using indexed arrays with preg_replace

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@113078 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Sara Golemon 2003-01-23 01:04:50 +00:00
parent 4d831c8b14
commit 4ee7faae08

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.4 $ -->
<!-- $Revision: 1.5 $ -->
<!-- splitted from ./en/functions/pcre.xml, last change in rev 1.2 -->
<refentry id="function.preg-replace">
<refnamediv>
@ -78,6 +78,64 @@ April1,2003
Every parameter to <function>preg_replace</function> (except
<parameter>limit</parameter>) can be an array.
</para>
<para>
<note>
<para>
When using arrays with <parameter>pattern</parameter> and
<parameter>replacement</parameter>, the keys are processed
in the order they appear in the array. This is
<emphasis>not necessarily</emphasis> the same as the numerical
index order. If you use indexes to identify which
<parameter>pattern</parameter> should be replaced by which
<parameter>replacement</parameter>, you should perform a
<function>ksort</function> on each array prior to calling
<function>preg_replace</function>.
</para>
</note>
<example>
<title>Using indexed arrays with <function>preg_replace</function></title>
<programlisting role="php">
<![CDATA[
<?php
$string = "The quick brown fox jumped over the lazy dog.";
$patterns[0] = "/quick/";
$patterns[1] = "/brown/";
$patterns[2] = "/fox/";
$replacements[2] = "bear";
$replacements[1] = "black";
$replacements[0] = "slow";
print preg_replace($patterns, $replacements, $string);
/* Output
======
The bear black slow jumped over the lazy dog.
*/
/* By ksorting patterns and replacements,
we should get what we wanted. */
ksort($patterns);
ksort($replacements);
print preg_replace($patterns, $replacements, $string);
/* Output
======
The slow black bear jumped over the lazy dog.
*/
?>
]]>
</programlisting>
</example>
</para>
<para>
If <parameter>subject</parameter> is an array, then the search
and replace is performed on every entry of