str_replace
字串取代。
语法: string str_replace(string needle, string str, string haystack);
传回值: 字串
函式种类: 资料处理
内容说明
本函式将字串 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;
?>
字串取代。
语法: string str_replace(string needle, string str, string haystack);
传回值: 字串
函式种类: 资料处理
内容说明
本函式将字串 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;
?>
[@str_replace("要替换的旧内容", "要取代原内容的新字符", $被替换内容的变量名)]
[@str_replace(array('旧1','旧2','旧3'), array('新1','新2','新3'), $被替换内容的变量名)]
[@str_replace(array('旧1','旧2','旧3'), '新内容', $被替换内容的变量名)] 多对一替换:想把内容字段里所有的<p></p>标签清除掉,替换成空 [@str_replace(array('<p>','</p>'), '', $Content)]
一对一替换:想把内容字段里所有的<br>标签换成<p> [@str_replace('<br>', '<p>', $Content)]
多对多替换:想把内容字段里的<br>换成<br />, 同时<p>换<hr>,把</p>全清除 [@str_replace(array('<br>', '<p>','</p>'), array('<br />','<hr>',''), $Content)] <?php
// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
echo $bodytag;
// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
echo $onlyconsonants;
// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
echo $newphrase;
// Use of the count parameter is available as of PHP 5.0.0
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count; // 2
$str="<p>this is test</p><br><p size=3>this.</p><p>hello</p>";
print str_replace(array('<p>','</p>','<br>'),array('<br>','<hr>',''),$str)
?>
本文来自: 脚本之家(www.jb51.net) 详细出处参考:http://www.jb51.net/article/13761.htm<?php
$arr1 = Array(
'http://img.jb51.net/img/offer/29/24/70/20/29247020',
'http://img.jb51.net/img/offer/29/24/70/20/29247020-1',
'http://img.jb51.net/img/offer/29/24/70/20/29247020-2'
);
$arr2 = Array(
'http://localhost/root/ups/af48056fc4.jpg',
'http://localhost/root/ups/cf33240aa3.jpg',
'http://localhost/root/ups/c30e40419b.jpg'
);
$data = '
<img src="http://img.jb51.net/img/offer/29/24/70/20/29247020"/>
<img src="http://img.jb51.net/img/offer/29/24/70/20/29247020-1"/>
<img src="http://img.jb51.net/img/offer/29/24/70/20/29247020-2"/>';
$data = str_replace($arr1,$arr2,$data);
var_dump($data);
?>
替换后的结果是: string(169) "<img src="http://localhost/root/ups/af48056fc4.jpg"/><img src="http://localhost/root/ups/af48056fc4.jpg-1"/><img src="http://localhost/root/ups/af48056fc4.jpg-2"/>"str_replace 函数的声明大概是这样: str_replace($search, $replace, $input[,&$count]), 比如在对一个字符串进行替换操作, $input 就是源字符串(称为数据源). 这很不合理,因为它把数据源放在第3位, 而 str_pos, strtok, str_repeat 等等函数都是把数据源放在第1位.也就是说str_replace并没有替换掉数组中相对应的字符串,而是把数组中的第一个替换,然后把相同的字符串后多余的合并。解决办法:function strrplace($arr1,$arr2,$data){ if(is_array($arr1)) { foreach($arr1 as $key => $value) { $data = str_replace_once($value, $arr2[$key], $data); } } return $data;}function str_replace_once($needle, $replace, $data) //替换第一次{ $pos = strpos($data, $needle); if ($pos === false) { return $data; } return substr_replace($data, $replace, $pos, strlen($needle));}
本文来自: 脚本之家(www.jb51.net) 详细出处参考:http://www.jb51.net/article/13761.htm