发表时间:2015-05-27来源:网络
(PHP 4, PHP 5)
str_replace ― Replace all occurrences of the search string with the replacement string
$search , mixed $replace , mixed $subject [, int &$count ] )This function returns a string or an array with all occurrences of search in subject replaced with the givenreplace value.
If you don't need fancy replacing rules (like regular expressions), you should always use this function instead ofpreg_replace().
If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string, then this replacement string is used for every value of search. The converse would not make sense, though.
If search or replace are arrays, their elements are processed first to last.
searchThe value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
replaceThe replacement value that replaces found search values. An array may be used to designate multiple replacements.
subjectThe string or array being searched and replaced on, otherwise known as the haystack.
If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well.
countIf passed, this will be set to the number of replacements performed.
This function returns a string or an array with the replaced values.
| Version | Description |
|---|---|
| 5.0.0 | The count parameter was added. |
| 4.3.3 | The behaviour of this function changed. In older versions a bug existed when using arrays as bothsearch and replace parameters which caused empty search indexes to be skipped without advancing the internal pointer on the replace array. This has been corrected in PHP 4.3.3, any scripts which relied on this bug should remove empty search values prior to calling this function in order to mimic the original behavior. |
| 4.0.5 | Most parameters can now be an array. |
Example #1 Basic str_replace() examples
// Provides:
$bodytag = str_replace("%body%", "black", "");
// 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");
// 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);
// Provides: 2
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count;
?>
Example #2 Examples of potential str_replace() gotchas
// Order of replacement
$str = "Line 1/nLine 2/rLine 3/r/nLine 4/n";
$order = array("/r/n", "/n", "/r");
$replace = '
';
// Processes /r/n's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $str);
// Outputs F because A is replaced with B, then B is replaced with C, and so on...
// Finally E is replaced with F, because of left to right replacements.
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);
// Outputs: apearpearle pear
// For the same reason mentioned above
$letters = array('a', 'p');
$fruit = array('apple', 'pear');
$text = 'a p';
$output = str_replace($letters, $fruit, $text);
echo $output;
?>Note: This function is binary-safe.
上一篇:PHP:计算小数点后位数
CI框架连接数据库配置操作以及多数据库操作
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
C语言关键字及其解释介绍 C语言32个关键字详解
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
将视频设置为Android手机开机动画的教程
PHP中的(++i)前缀自增 和 (i++)后缀自增
常用dos命令及语法
PHP中include和require区别之我见
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
计支宝app下载v3.1.29 安卓版
142.27MB |商务办公
小小优酷最新版下载v5.4.6 安卓官方版
68.11MB |影音播放
艺图语app下载v3.3.4 安卓免费版
34.33MB |系统工具
爱鸽者手机版下载v3.2.9 安卓版
243.57MB |商务办公
一品威客网接单app(一品众包)下载v2.7.1 安卓最新版
55.01MB |商务办公
诸葛找房网官方版下载v4.8.1.1 安卓最新版
66.6MB |生活服务
皓盘云建最新版下载v9.0 安卓版
53.38MB |商务办公
ris云客移动销售系统最新版下载v1.1.25 安卓手机版
42.71M |商务办公
2022-03-17
2014-09-05
2022-03-20
2022-03-24
2014-09-05
2015-07-05
2014-09-05
2014-09-05
2014-09-05
2022-03-21
葫芦侠三楼最新破解版下载v4.4.0.6 安卓版
其它手游葫芦侠7楼破解版免费下载v4.4.0.6安卓最新版
其它手游葫芦侠八楼免费版下载v4.4.0.6安卓版
其它手游葫芦侠3楼破解版免费下载v4.4.0.6安卓手机版
其它手游像素漫斗抱歉,“八神庵”是特指游戏角色的专有名词,不存在符合要求的常用同义词近义词。下载v1.00安卓版
其它手游葫芦侠4楼破解版游戏修改器下载v4.4.0.6安卓免费版
其它手游末日餐厅安卓版下载v1.35安卓版
其它手游葫芦侠十楼免费破解版下载v4.4.0.6安卓版
其它手游葫芦侠8楼最新破解版下载v4.4.0.6安卓版
其它手游