PHP str_replace() 函数 & strtr() 函数 的常见用法

php str_replace定义和用法


str_replace() 函数使用一个字符串替换字符串中的另一些字符。
php str_replace语法

str_replace(from_str, to_str, string, count)

from_str: 替换的字符串

to_str: 替换的结果

string:指定操作的字符串(备注:该值也可以是一个array数组)
count:替换的最大次数,这个参数是可选参数


<?php
/*Function:
    mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count] )
*/
  
//1==>
// 输出: <body text='black'>
/*
    这应该是最常见的用法了,从"<body text='%body%'>"中找到"%body%",然后替换成"black"
*/
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
  
//2==>
// 输出: Hll Wrld f PHP
/*
    参数 search 为数组的用法,逐个地遍历数组来进行替换
*/
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
  
//3==>
// 输出: You should eat pizza, beer, and ice cream every day
/*
    参数 search 和 replace 均为数组的用法,且数组元素的个数相同,进行相互对应的替换
*/
$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);
  
//4==>
// 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
  
//5==>
// Order of replacement
/*
    自定义替换顺序,防止重复替换
*/
$str       = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order    = array("\r\n", "\n", "\r");
$replace = '<br />';
// Processes \r\n's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $str);
  
//6==>
/*
    这个是需要注意的,参数 search 和 replace 均为数组,第一次先替换 'a' ,结果为 'apple p',第二次接着替换 'p',因此会出现结果 'apearpearle pear'
*/
// Outputs: apearpearle pear
$letters  = array('a', 'p');
$fruit     = array('apple', 'pear');
$text     = 'a p';
$output  = str_replace($letters, $fruit, $text);
echo $output;
?>


strtr() 函数转换字符串中特定的字符


语法
strtr(string,from,to)或者
strtr(string,array)参数 描述
string1 必需。规定要转换的字符串。
from 必需(除非使用数组)。规定要改变的字符。
to 必需(除非使用数组)。规定要改变为的字符。
array 必需(除非使用 from 和 to)。一个数组,其中的键是原始字符,值是目标字符。
说明
如果 from 和 to 的长度不同,则格式化为最短的长度。
例子
例子 1

<?php
echo strtr("Hilla Warld","ia","eo");
?>

输出:
Hello World例子 2

<?php
$arr = array("Hello" => "Hi", "world" => "earth");
echo strtr("Hello world",$arr);
?>

输出:
Hi earth



string strtr ( string $str , string $from , string $to )
string strtr ( string $str , array $replace_pairs )
当使用第一种的时候, 参数 $from, $to 的字符串长度一定要相同, 否则多余的(不管是$from多还是$to多) 字符被忽略.


比如 $str = 'a-=b' ;


当$from='-=' ,$to='CD',输出'aCDb', 因为'-='与'CD'的长度相同,没有问题.


当$from='-=' ,$to='CDE',输出'aCDb', $to里的'E'被忽略.

当$from='-=' ,$to='C',输出'aC=b', $from里的'='被忽略.

而使用第二种形式, 则没有这个问题, 多余的字条不会忽略.


所以,如果故意用 strtr 函数代替 str_replace, 并且使用了第一种形式, 则一定要注意这个特征, 这可能是一个陷阱.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值