(PHP 4, PHP 5, PHP 7)
str_replace — 锟斤拷锟街凤拷锟斤拷锟芥换
锟矫猴拷锟斤拷锟斤拷锟斤拷一锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟介。锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟角斤拷 subject
锟斤拷全锟斤拷锟斤拷 search
锟斤拷锟斤拷 replace
锟芥换之锟斤拷慕锟斤拷锟斤拷
锟斤拷锟矫伙拷锟揭恍╋拷锟斤拷锟斤拷锟芥换锟斤拷锟襟(憋拷锟斤拷锟斤拷锟斤拷锟斤拷式锟斤拷锟斤拷锟斤拷应锟斤拷使锟矫该猴拷锟斤拷锟芥换 ereg_replace() 锟斤拷 preg_replace()锟斤拷
锟斤拷锟� search
锟斤拷 replace
为锟斤拷锟介,锟斤拷么 str_replace() 锟斤拷锟斤拷 subject
锟斤拷锟斤拷锟竭碉拷映锟斤拷锟芥换锟斤拷锟斤拷锟� replace
锟斤拷值锟侥革拷锟斤拷锟斤拷锟斤拷 search
锟侥革拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷婊伙拷锟绞癸拷每锟斤拷址锟斤拷锟斤拷锟斤拷锟斤拷小锟斤拷锟斤拷 search
锟斤拷一锟斤拷锟斤拷锟斤拷锟� replace
锟斤拷一锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷么 search
锟斤拷每锟斤拷元锟截碉拷锟芥换锟斤拷始锟斤拷使锟斤拷锟斤拷锟斤拷址锟斤拷锟斤拷锟斤拷锟阶拷锟斤拷锟斤拷锟侥憋拷锟叫⌒达拷锟�
锟斤拷锟� search
锟斤拷 replace
锟斤拷锟斤拷锟斤拷锟介,锟斤拷锟角碉拷值锟斤拷锟结被锟斤拷锟轿达拷锟斤拷
search
锟斤拷锟揭碉拷目锟斤拷值锟斤拷也锟斤拷锟斤拷 needle锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟街革拷锟斤拷锟斤拷目锟疥。
replace
search
锟斤拷锟芥换值锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷员锟斤拷锟斤拷锟街革拷锟斤拷锟斤拷锟斤拷婊伙拷锟�
subject
执锟斤拷锟芥换锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷址锟斤拷锟斤拷锟揭诧拷锟斤拷锟� haystack锟斤拷
锟斤拷锟� subject
锟斤拷一锟斤拷锟斤拷锟介,锟芥换锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 subject
锟斤拷锟斤拷锟斤拷值也锟斤拷锟斤拷一锟斤拷锟斤拷锟介。
count
锟斤拷锟斤拷锟街革拷锟斤拷锟斤拷锟斤拷锟街碉拷锟斤拷锟斤拷锟斤拷锟轿拷婊伙拷锟斤拷锟斤拷拇锟斤拷锟斤拷锟�
锟矫猴拷锟斤拷锟斤拷锟斤拷锟芥换锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷
Example #1 str_replace() 锟斤拷锟斤拷锟斤拷锟斤拷
<?php
// 锟斤拷值: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
// 锟斤拷值: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
// 锟斤拷值: 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);
// 锟斤拷值: 2
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count;
?>
Example #2 锟斤拷锟杰碉拷 str_replace() 锟芥换锟斤拷锟斤拷
<?php
// 锟芥换顺锟斤拷
$str = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order = array("\r\n", "\n", "\r");
$replace = '<br />';
// 锟斤拷锟斤拷锟芥换 \r\n 锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷遣锟斤拷岜伙拷锟斤拷锟阶拷锟�
$newstr = str_replace($order, $replace, $str);
// 锟斤拷锟� F 锟斤拷锟斤拷为 A 锟斤拷 B 锟芥换锟斤拷B 锟街憋拷 C 锟芥换锟斤拷锟皆达拷锟斤拷锟斤拷...
// 锟斤拷锟节达拷锟斤拷锟斤拷锟斤拷锟斤拷锟芥换锟斤拷锟斤拷锟斤拷 E 锟斤拷 F 锟芥换
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);
// 锟斤拷锟�: apearpearle pear
// 锟斤拷锟斤拷锟斤拷锟斤拷锟结到锟斤拷原锟斤拷
$letters = array('a', 'p');
$fruit = array('apple', 'pear');
$text = 'a p';
$output = str_replace($letters, $fruit, $text);
echo $output;
?>
Note: 锟剿猴拷锟斤拷锟缴帮拷全锟斤拷锟节讹拷锟斤拷锟狡讹拷锟斤拷
锟斤拷锟斤拷 str_replace() 锟斤拷锟芥换时锟斤拷锟斤拷锟斤拷锟斤拷锟轿斤拷锟叫的o拷锟斤拷锟叫讹拷锟斤拷锟芥换锟斤拷时锟斤拷锟斤拷芑锟斤拷婊伙拷锟街帮拷锟斤拷锟斤拷值锟斤拷锟轿硷拷锟斤拷锟侥碉拷锟侥凤拷锟斤拷锟斤拷
Note:
锟矫猴拷锟斤拷锟斤拷锟街达拷小写锟斤拷使锟斤拷 str_ireplace() 锟斤拷锟皆斤拷锟叫诧拷锟斤拷锟街达拷小写锟斤拷锟芥换锟斤拷