【www.gdgbn.com--js教程】

str_replace只替换一次代码

$str ="中国|111cn.net|111cn.net|111cn.net|jkldajfklda李好,美女,世界中国中国中国

中国中国abc,dee";
$str1=array(
  array("111cn.net","/phper.html"),
  array("中国","/phper.html"),
  array("李好","/phper.html"),
  array("dee","/phper.html"),
  array("abc","/phper.html")
  );
//$temp = str_replace("111cn.net","前程似锦",$str,$a);
    $count =0;
 foreach($str1 as $nkeys){    
   if(strpos($str,$nkeys[0]) ){
    if( $count <=1 ){
     $str=preg_replace("/$nkeys[0]/","

href=http://111cn.net.com".$nkeys[1]." target=_blank >".$nkeys

[0]."",$str,1);     $count++;
     continue ;
    }
    
    
    
   }
    
  }
 echo $count,$str;
//preg_replace(【要替换的关键字】, 【替换为的关键字】, 【原字符串】, 【替换次数

】);
//方法二:

function str_replace_once($needle, $replace, $haystack) {

   $pos = strpos($haystack, $needle);

   if ($pos === false) {

      return $haystack;

   }

   return substr_replace($haystack, $replace, $pos, strlen($needle));

}

本文来源:http://www.gdgbn.com/wangyezhizuo/21250/