php replace all,php之str_replace详解_php

If passed, this will be set to the number of replacements performed.

Return Values

This function returns a string or an array with the replaced values.

Changelog

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.

Examples

Example #1 Basic str_replace() examples

// 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

// 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;

?>

Notes

Note: This function is binary-safe.

欢迎大家阅读《php之str_replace详解_php》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

e7ce419cf2d6ad34d01da2ceb8829eed.png

微信 赏一包辣条吧~

023a57327877fb4402bcc76911ec18ea.png

支付宝 赏一听可乐吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值