php各种替换字符串函数,两种php 字符串替换函数

本文章主要讲到两种php 字符串替换函数 str_replace与substr_replace前一种是替换所有haystace中的needle,并返回haystace替换后的结果,而substr_replace替换字符串string中的一部分具体哪能一部分则取决于起始位置和可选参数length的值

最常用str_replace()

函数原型:

mixed str_replace(mixed needle,mixed new_needle,mixed haystace[,int &count])

函数用new_needle替换所有haystace中的needle,并返回haystace替换后的结果.可选的第四个参数count,它包含要执行的替换操作次数.

例:

 代码如下复制代码
  <?php

function n2n() {

=array('1','2','3','4','5','6','7','8','9','0');

=array("7","8","9","0","1","2","3","4","5","6");

=strlen();

for(=0;

for(=0;<10; )

if(==){

= str_replace(, , );

break;

}

}

return ;

}

="1234abc56";

echo n2n();

?>

函数substr_replace()则用来在给定位置中查找和替换字符串中特定的子字符串.原型:

string substr_replace(string string,string replacement,int start,int[length]);

 代码如下复制代码
 <?php

echo substr_replace("hello world","earth",6);//hello earth

?>

负数时

 代码如下复制代码
 <?php

substr_replace('huevos','x',-2,-2); //huevxos

substr_replace('huevos','x',-2,-3); //huevxos

substr_replace('huevos','x',-2,-3); //huevxos

?>

函数使用字符串replacement替换字符串string中的一部分具体哪能一部分则取决于起始位置和可选参数length的值.

start的值代表要替换字符串位置的开始偏移量.如果它为0或是一个正值,就是一个从字符串开始处计算的偏移量;如果它是一个负值,就是从字

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值