str_replace的语法

如题所述

第1个回答  2016-05-27

str_replace(find,replace,string,count) 参数 描述 find 必需。规定要查找的值。 replace 必需。规定替换 find 中的值的值。 string 必需。规定被搜索的字符串。 count 可选。一个变量,对替换数进行计数。 本函数将字符串 str 代入 haystack 字符串中,将所有的 needle 置换成 str。[email protected] (11-Apr-1999) 指出在 PHP 3.0.7 版
本函数有些 bug,而 [email protected] (05-Jun-1999) 补充在 PHP 3.0.8 版本函数就回复正常了。 下例将 %body% 以 black 取代
<?php
$bodytag = str_replace(%body%, black, <body text=%body%>);
echo $bodytag;
?>
参考
ereg_replace()
提示和注释
注释:该函数对大小写敏感。请使用 str_ireplace() 执行对大小写不敏感的搜索。
注释:该函数是二进制安全的。
例子
例子 1
<?php
echo str_replace(world,John,Hello world!);
?>输出:
Hello John!例子 2
在本例中,我们将演示带有数组和 count 变量的 str_replace() 函数:
<?php
$arr = array(blue,red,green,yellow);
print_r(str_replace(red,pink,$arr,$i));
echo Replacements: $i;
?>输出:
Array
(
[0] => blue
[1] => pink
[2] => green
[3] => yellow
)
Replacements: 1例子 3
<?php
$find = array(Hello,world);
$replace = array(B);
$arr = array(Hello,world,!);
print_r(str_replace($find,$replace,$arr));
?>输出:
Array
(
[0] => B
[1] =>
[2] => !
)

相似回答