php实现字符串替换
一、总结
二、php实现字符串替换
代码一:
1 //字符串替换 2 function str_replace($substr , $newsubstr, $str) 3 { 4 $m = strlen($str); 5 $n = strlen($substr ); 6 $x = strlen($newsubstr ); 7 if (strchr($str, $substr ) == false) return false; //查找字符串 8 for ( $i=0; $i<=($m- $n+1); $i++){ 9 $i = strchr($str, $substr); 10 $str = str_delete ($str, $i, $n); //删除字符串 11 $str = str_insert($str, $i, $newstr); //插入字符串 12 } 13 return $str ; 14 }
代码二: