zencart産品描述加上錨文本

首先,函數會遍曆整段描述,假如一段描述裏面有Hermes wallets這個關鍵詞,那麽函數就會對這個關鍵詞加上鏈接,至于鏈接到哪裏,上面數組裏面有,隻要把數組裏面的内容替換你想要的就可以。

那麽在zencart模闆上怎樣使用此函數呢?你要知道,輸出産品描述的函數是 stripslashes($products_description)

隻要在此函數上再嵌套一個函數即可,像這樣zen_key_replace(stripslashes($products_description))


<?php //自动匹配关键词,遇到相同的关键词的就插入链接
//function zen_key_replace($str){
$array_first = array(
array('Hermes wallets','link1.html'),
array('black ugg boots','link2.html'),
array('cheap ugg boots','link3.html'),
array('cheapest ugg boots','link4.html'),
array('discount ugg boots','link5.html'),
array('girls ugg boots','link6.html'),
array('grey ugg boots','link7.html'),
array('ugg boots','link8.html')
);
$array_last = array();
foreach($array_first as $key=>$value){

//echo $key."= ".$value."<br>";
$array_last[$key] = array('TFXS00'.$key, $value[0], '<a href="'.$value[1].'" title="'.$value[0].'" rel="follow">'.$value[0].'</a>');
//$array_last[$key]."<br>";
}
//print_r($array_last);
//exit;

$count = count($array_last);
for($i=0; $i<$count; $i++){
for($j=$count-1; $j>$i; $j--){
//如果后一个元素长度大于前一个则调换位置
if(strlen($array_last[$j][1]) > strlen($array_last[$j-1][1])){
$tmp = $array_last[$j];
$array_last[$j] = $array_last[$j-1];
$array_last[$j-1] = $tmp;
}
}
}
$keys=$array_last;
print_r($keys);


foreach($keys as $nkeys){
$str= str_ireplace($nkeys[1], $nkeys[0], $str);
}
foreach($keys as $nkeys){
$str= str_ireplace($nkeys[0], $nkeys[2], $str);
}
return $str;
}
?>

posted @ 2013-09-23 18:14  外贸乐  阅读(272)  评论(0编辑  收藏  举报