打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
PHP字符串替换str

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
该函数返回一个字符串或者数组。该字符串或数组是将subject中全部的search都被replace替换之后的结果。
1、$search,要替换的字符串,或数组
2、$replace,被用来替换的字符串或数组
3、$subject,被查询的字符串或数组
4、$count,可选,如果被指定,将为设置为替换的次数
5、返回值:该函数返回替换后的数组或者字符串(新生成的)

<?php    //实例一:字符串替换字符串    $str1 = str_replace("red","black","red green yellow pink purple");    echo $str1."";    //输出结果为black green yellow pink purple?>
<?php    //实例二:字符串替换数组键值    $arr = array("blue","red","green","yellow");    $str1 = str_replace("red","pink",$arr,$i);    print_r($str1);?>
<?php    //实例三:数组替换数组,映射替换    $arr1 = array("banana","orange");    $arr2 = array("pitaya","tomato");    $con_arr = array("apple","orange","banana","grape");    $con_rep = str_replace($arr1,$arr2,$con_arr,$count);    print_r($con_rep);?>
<?php    //实例四:如$search为数组,$replace为字符串时    $search = array("banana","grape");    $replace = "tomato";    $arr = array("banana","apple","orange","grape");    $new_arr = str_replace($search,$replace,$arr,$count);    print_r($new_arr);?>

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
php将指定字符串替换的方法
PHP 字符串替换 substr
str_replace
preg_replace()参数均为数组多次替换的实例理解
PHP常用 API
PHP中数组的区别
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服