本文章主要讲到两种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]);
|