-
-
/** - * Content keyword replacement
- * by bbs.it-home.org
- */
$arr=array(
- "shoes"=>"Shoes",
- "Socks"=>"Socks",
- "Skirt"=> ;"skirt",
- "dress"=>"dress",
- );
- $str="shoes socks shoes socks shoes socks shoes socks skirt dress";
- $str=replacelink($str,$arr,3);
- echo $str;
public function replacelink($str,$arr,$maxcount=3000){
- //Match the picture
- preg_match_all("/(]*>)/iUs",$str,$imgs) ;
- preg_match_all("/(.*)/iUs",$str,$links);
- //Replace pictures and links
- if($links[1]){
- foreach($links[1] as $k=>$v){
- $str=str_replace($v,"@links_$k",$str);
- }
- }
- if($imgs[1] ){
- foreach($imgs[1] as $k=>$v){
- $str=str_replace($v,"@imgs_$k",$str);
- }
- }
- //End of matching
- $str=strtr($str,$arr);
- preg_match_all("/(.*)/iUs",$str,$a);
- $a=$a [1];
- $c=array_count_values($a);
- if($c){
- $kk=0;
- foreach($c as $k=>$v){
- if($v){
- if($kk<$maxcount){
- $v=$v-1;
- }
- $str=preg_replace("/".$this->replace_quote($k)."/i",str_replace("# ","",strip_tags($k)),$str,$v);
- }
- $kk++;
- }
- }
- //Restore pictures and links
- if($links[1]){
- foreach($ links[1] as $k=>$v){
- $str=str_replace("@links_$k",$v,$str);
- }
- }
- if($imgs[1]){
- foreach ($imgs[1] as $k=>$v){
- $str=str_replace("@imgs_$k",$v,$str);
- }
- }
- return $str;
- } p>
function replace_quote($str){
- $str=preg_quote($str);
- $str=str_replace("/","/",$str);
- return $str;
- }< ;/p>
-
Copy code
|