晓夏

北漂的女孩

Good Luck To You!

php str_replace 多个词语查找替换

浏览量:453

<?php
function replace($string,$keyArray,$replacement,$i){ 
   $result=''; 
   if($i<(count($keyArray))){ 
     $strSegArray=explode($keyArray[$i],$string); 
    foreach ($strSegArray as $index=>$strSeg){ 
     $x=$i+1; 
     if($index==(count($strSegArray)-1)) 
       $result=$result.replace($strSeg,$keyArray,$replacement,$x); 
     else
     $result=$result.replace($strSeg,$keyArray,$replacement,$x).$replacement[$i];                 
    }      
    return $result; 
   } 
  else{
    return $string; 
  } 
} 
$string=' PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。PHP 是免费的,并且使用广
泛。对于像微软 ASP 这样的竞争者来说,PHP 无疑是另一种高效率的选项。'; 

$keyArray=array('PHP','交互性','高效率'); 
$replacement=array('AAAA','BBBB','CCCC'); 
echo replace($string,$keyArray,$replacement,0);
?>


神回复

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。