reference/strings/functions/str-replace.xml
45042fef652f1b4e904e809fcbfcf31f6c60670b
45042fef652f1b4e904e809fcbfcf31f6c60670b
...
...
@@ -1,6 +1,6 @@
1
1
<?xml version="1.0" encoding="utf-8"?>
2
2
<!-- $Revision$ -->
3
-
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.str-replace">
3
+
<refentry xml:id="function.str-replace" xmlns="http://docbook.org/ns/docbook">
4
4
<refnamediv>
5
5
<refname>str_replace</refname>
6
6
<refpurpose>Replace all occurrences of the search string with the replacement string</refpurpose>
...
...
@@ -9,11 +9,11 @@
9
9
<refsect1 role="description">
10
10
&reftitle.description;
11
11
<methodsynopsis>
12
-
<type>mixed</type><methodname>str_replace</methodname>
13
-
<methodparam><type>mixed</type><parameter>search</parameter></methodparam>
14
-
<methodparam><type>mixed</type><parameter>replace</parameter></methodparam>
15
-
<methodparam><type>mixed</type><parameter>subject</parameter></methodparam>
16
-
<methodparam choice="opt"><type>int</type><parameter role="reference">count</parameter></methodparam>
12
+
<type class="union"><type>string</type><type>array</type></type><methodname>str_replace</methodname>
13
+
<methodparam><type class="union"><type>array</type><type>string</type></type><parameter>search</parameter></methodparam>
14
+
<methodparam><type class="union"><type>array</type><type>string</type></type><parameter>replace</parameter></methodparam>
15
+
<methodparam><type class="union"><type>string</type><type>array</type></type><parameter>subject</parameter></methodparam>
16
+
<methodparam choice="opt"><type>int</type><parameter role="reference">count</parameter><initializer>&null;</initializer></methodparam>
17
17
</methodsynopsis>
18
18
<para>
19
19
This function returns a string or an array with all occurrences of
...
...
@@ -21,8 +21,8 @@
21
21
replaced with the given <parameter>replace</parameter> value.
22
22
</para>
23
23
<para>
24
-
If you don't need fancy replacing rules (like regular expressions), you
25
-
should always use this function instead of <function>preg_replace</function>.
24
+
To replace text based on a pattern rather than a fixed
25
+
string, use <function>preg_replace</function>.
26
26
</para>
27
27
</refsect1>
28
28
...
...
@@ -107,10 +107,12 @@
107
107
<?php
108
108
// Provides: <body text='black'>
109
109
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
110
+
echo $bodytag, PHP_EOL;
110
111
111
112
// Provides: Hll Wrld f PHP
112
113
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
113
114
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
115
+
echo $onlyconsonants, PHP_EOL;
114
116
115
117
// Provides: You should eat pizza, beer, and ice cream every day
116
118
$phrase = "You should eat fruits, vegetables, and fiber every day.";
...
...
@@ -118,10 +120,11 @@ $healthy = array("fruits", "vegetables", "fiber");
118
120
$yummy = array("pizza", "beer", "ice cream");
119
121
120
122
$newphrase = str_replace($healthy, $yummy, $phrase);
123
+
echo $newphrase, PHP_EOL;
121
124
122
125
// Provides: 2
123
126
$str = str_replace("ll", "", "good golly miss molly!", $count);
124
-
echo $count;
127
+
echo $count, PHP_EOL;
125
128
?>
126
129
]]>
127
130
</programlisting>
...
...
@@ -140,13 +143,14 @@ $replace = '<br />';
140
143
141
144
// Processes \r\n's first so they aren't converted twice.
142
145
$newstr = str_replace($order, $replace, $str);
146
+
echo $newstr, PHP_EOL;
143
147
144
148
// Outputs F because A is replaced with B, then B is replaced with C, and so on...
145
149
// Finally E is replaced with F, because of left to right replacements.
146
150
$search = array('A', 'B', 'C', 'D', 'E');
147
151
$replace = array('B', 'C', 'D', 'E', 'F');
148
152
$subject = 'A';
149
-
echo str_replace($search, $replace, $subject);
153
+
echo str_replace($search, $replace, $subject), PHP_EOL;
150
154
151
155
// Outputs: apearpearle pear
152
156
// For the same reason mentioned above
...
...
@@ -154,7 +158,7 @@ $letters = array('a', 'p');
154
158
$fruit = array('apple', 'pear');
155
159
$text = 'a p';
156
160
$output = str_replace($letters, $fruit, $text);
157
-
echo $output;
161
+
echo $output, PHP_EOL;
158
162
?>
159
163
]]>
160
164
</programlisting>
...
...
@@ -194,7 +198,6 @@ echo $output;
194
198
</refsect1>
195
199
196
200
</refentry>
197
-
198
201
<!-- Keep this comment at the end of the file
199
202
Local variables:
200
203
mode: sgml
201
204