-
09月22日
-
【函数功能】
该函数将指定的字符串中的字符或字符串转换成指定的字符或字符串。
【函数语法】
strtr ($str, $from, $to)/strtr ($str, $array)
【参数说明】
分下面两种情况:
1.使用strtr ($str, $from, $to)格式: $str:必需,要转换的字符串。 $from:必需,要改变的字符。 $to:必需,要改变为的字符。 其中$from和$to要有对应关系,例如$from = "abc",$to = "def",则替换规则是:将$str中的a替换为d,b替换为e,c替换为f。,如果$from和$to长度不一样,即无法一一对应,此时以长度短的为准。在这种格式下使用中文会出现乱码。
2.使用strtr ($str, $array)格式: $array:必需,数组,其中的键名是更改的原始字符,键值是更改的目标字符。例如$array = array("abc"=>"def"),此时,会将$str中的abc字符串替换为def,这种情况下是将字符串替换为字符串,不再是一一对应的字符替换,因此可以将字符串替换为中文。
【演示程序】
<?php /** * strtr ($str, $from, $to)/strtr ($str, $array) * **/ $str = "hello i am PHP!"; echo strtr($str, "hl", "HL"); echo "<br>"; echo strtr($str, array('hello'=>'大家好')); ?>
【输出结果】
HeLLo i am PHP! 大家好 i am PHP!
【特别注意】
1.若使用数组替换,则当数组包含空键名或者为空时返回false。 2.不使用数组格式的时候不能替换中文,会出现乱码。 3.PHP 版本:4+。
【原版定义】
/** * Translate characters or replace substrings * @link http://www.php.net/manual/en/function.strtr.php * @param str string <p> * The string being translated. * </p> * @param from string <p> * The string being translated to to. * </p> * @param to string <p> * The string replacing from. * </p> * @return string the translated string. * </p> * <p> * If replace_pairs contains a key which * is an empty string (""), * false will be returned. If the str is not a scalar * then it is not typecasted into a string, instead a warning is raised and * &null; is returned. */
转载请注明出处:php1234.cn ,原文地址:http://www.php1234.cn/a/functions/2016/0922/74.html
收藏文章

登录
表情删除后不可恢复,是否删除
取消
确定
图片正在上传,请稍后...
取消上传
评论内容为空!
还没有评论,快来抢沙发吧!
热评话题
- 德者,人之所得,使万物各得其所欲_PHP1234|PHP修炼之路|跟着明礼馨德一起修炼PHP
- 义者,人之所宜,赏善罚恶,以立功立事_PHP1234|PHP修炼之路|跟着明礼馨德一起修炼PHP
- 夫欲为人之本,不可无一焉_PHP1234|PHP修炼之路|跟着明礼馨德一起修炼PHP
- 潜居抱道,以待其时!_PHP1234|PHP修炼之路|跟着明礼馨德一起修炼PHP
- 极人臣之位;成绝代之功;如其不遇,没身而已_PHP1234|PHP修炼之路|跟着明礼馨德一起修炼PHP
- 明于盛衰之道,通乎成败之数;审乎治乱之事,_PHP1234|PHP修炼之路|跟着明礼馨德一起修炼PHP
- 仁者,人之所亲,有慈惠恻隐之心,以遂其生成_PHP1234|PHP修炼之路|跟着明礼馨德一起修炼PHP