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;
?>

猜你喜欢

转载自wddpwzzhao123.iteye.com/blog/2278033
今日推荐