您好,欢迎访问一九零五行业门户网

php网页tag标签替换单元测试代码

<?php class tagreplaceapi{ public function tagreplace($content, $tags){//替换内容及所有tags $replacearr = array(); $tagslength=count($tags); for($i=0; $i<$tagslength; $i++) { $content = $this -> tagreplacecontent($content, $tags[$i], $replacearr, $i); } foreach($replacearr as $key=>$value) { //echo "key=" . $key . ", value=" . $value . "<br/>"; $content = str_replace($key, $value, $content); } return $content; } public function tagreplacecontent($content, $tag, &$replacearr, $i){//替换内容及tag $tagname = $tag['tagname']; $tagurl = $tag['tagurl']; //echo "tagname=" . $tagname . ", tagurl=" . $tagurl . "<br/>"; $revalue = "<a href=\"".$tagurl."\">".$tagname."</a>"; $content = str_replace($revalue, $tagname, $content); $rekey = "######" . $i . "######"; $content = str_replace($tagname, $rekey, $content); $replacearr[$rekey] = $revalue; return $content; } } $content = '一般不注意小米觉得再正常不过的小米正品黑色手环一张小米正品活塞耳机2代咖啡金色照片,不过仔细观察小米正品黑色手环会发现最令人小米惊叹的是一小米正品活塞耳机3代黑色只正在经过的飞鸟,被十分清晰地抓拍了下来,没有丝毫模糊,要知道手机拍摄动态物体的实际成像效果一般都不怎么理小米正品活塞耳机2代咖啡金色想的,更何况是一只正在飞翔的大鸟,画面并没有出现抖动情况,很清晰,由此可见小米5在运动抓拍方面应该很小米给力。'; $tags = array(array('tagname' => '小米正品活塞耳机2代咖啡金色','tagurl' => 'http://www.mi.com/kfj/'), array('tagname' => '小米正品活塞耳机3代黑色','tagurl' => 'http://www.mi.com/hei/'), array('tagname' => '小米','tagurl' => 'http://www.mi.com/')); $tagreplaceapi = new tagreplaceapi(); $content = $tagreplaceapi -> tagreplace($content, $tags); echo $content; echo "<br/>"; echo "<br/>"; $content = $tagreplaceapi -> tagreplace($content, $tags); echo $content; echo "<br/>"; echo "<br/>"; $content = $tagreplaceapi -> tagreplace($content, $tags); echo $content; ?>
其它类似信息

推荐信息