reference/pcre/functions/preg-replace.xml
d6f54016d62904cfd8200604aadd5e3f0d9bad97
...
...
@@ -21,6 +21,11 @@
21
21
<parameter>pattern</parameter> and replaces them with
22
22
<parameter>replacement</parameter>.
23
23
</para>
24
+
<para>
25
+
To match an exact string, rather than a pattern,
26
+
consider using <function>str_replace</function> or
27
+
<function>str_ireplace</function> instead of this function.
28
+
</para>
24
29
</refsect1>
25
30

26
31
<refsect1 role="parameters">
...
...
@@ -103,6 +108,10 @@
103
108
replace is performed on every entry of <parameter>subject</parameter>,
104
109
and the return value is an array as well.
105
110
</para>
111
+
<para>
112
+
If the <parameter>subject</parameter> array is associative, keys
113
+
will be preserved in the returned value.
114
+
</para>
106
115
</listitem>
107
116
</varlistentry>
108
117
<varlistentry>
...
...
@@ -148,6 +157,7 @@
148
157
Using the "\e" modifier is an error;
149
158
an <constant>E_WARNING</constant> is emitted in this case.
150
159
</para>
160
+
&pcre.pattern.warning;
151
161
</refsect1>
152
162

153
163
<refsect1 role="examples">
...
...
@@ -204,6 +214,15 @@ The bear black slow jumps over the lazy dog.
204
214
<programlisting role="php">
205
215
<![CDATA[
206
216
<?php
217
+
$string = 'The quick brown fox jumps over the lazy dog.';
218
+
$patterns = array();
219
+
$patterns[0] = '/quick/';
220
+
$patterns[1] = '/brown/';
221
+
$patterns[2] = '/fox/';
222
+
$replacements = array();
223
+
$replacements[2] = 'bear';
224
+
$replacements[1] = 'black';
225
+
$replacements[0] = 'slow';
207
226
ksort($patterns);
208
227
ksort($replacements);
209
228
echo preg_replace($patterns, $replacements, $string);
...
...
@@ -295,6 +314,28 @@ xp***to
295
314
<function>preg_replace</function>.
296
315
</para>
297
316
</note>
317
+
<note>
318
+
<para>
319
+
When both <parameter>pattern</parameter> and <parameter>replacement</parameter> are
320
+
arrays, matching rules will operate sequentially. That is, the second <parameter>pattern</parameter>/<parameter>replacement</parameter>
321
+
pair will operate on the string that results from the first <parameter>pattern</parameter>/<parameter>replacement</parameter>
322
+
pair, not the original string. If you want to simulate replacements operating in parallel,
323
+
such as swapping two values, replace one pattern by an intermediary placeholder, then in a
324
+
later pair replace that intermediary placeholder with the desired replacement.
325
+
</para>
326
+
<informalexample>
327
+
<programlisting role="php">
328
+
<![CDATA[
329
+
<?php
330
+
$p = array('/a/', '/b/', '/c/');
331
+
$r = array('b', 'c', 'd');
332
+
print_r(preg_replace($p, $r, 'a'));
333
+
// prints d
334
+
?>
335
+
]]>
336
+
</programlisting>
337
+
</informalexample>
338
+
</note>
298
339
</refsect1>
299
340

300
341
<refsect1 role="seealso">
...
...
@@ -308,6 +349,7 @@ xp***to
308
349
<member><function>preg_replace_callback</function></member>
309
350
<member><function>preg_split</function></member>
310
351
<member><function>preg_last_error</function></member>
352
+
<member><function>str_replace</function></member>
311
353
</simplelist>
312
354
</para>
313
355
</refsect1>
314
356