PHP字符串替换函数 可同时替换多个关键词

原创
2016-07-25 08:55:40 1040浏览
  1. /**
  2. * 字符串替换函数
  3. * edit: bbs.it-home.org
  4. */
  5. function replace($string,$keyArray,$replacement,$i){
  6. $result='';
  7. if($i $strSegArray=explode($keyArray[$i],$string);
  8. foreach ($strSegArray as $index=>$strSeg){
  9. $x=$i+1;
  10. if($index==(count($strSegArray)-1))
  11. $result=$result.replace($strSeg,$keyArray,$replacement,$x);
  12. else
  13. $result=$result.replace($strSeg,$keyArray,$replacement,$x).$replacement[$i];
  14. }
  15. return $result;
  16. }
  17. else{
  18. return $string;
  19. }
  20. }
复制代码

代码说明: $string=' 键名 数组可以同时含有 integer 和 string 类型的键名,12345678 因为 PHP 实际并不区分索引数组和关联数组。 如果对给出的值没有指定键名,则取当前最大的整数索引值,而新的键名将是该值加一。如果指定的键名已经有了值,则该值会被覆盖。'; 示例:

  1. $keyArray=array('数组','integer','2345','键名');
  2. $replacement=array('AAAA','BBBB','CCCC','DDDD');
  3. echo replace($string,$keyArray,$replacement,0);
复制代码


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。