PHP web page tag tag replacement unit test code

巴扎黑
Release: 2023-03-03 12:12:01
Original
1277 people have browsed it

<?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[&#39;tagName&#39;];
$tagUrl = $tag[&#39;tagUrl&#39;];
//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 = &#39;一般不注意小米觉得再正常不过的小米正品黑色手环一张小米正品活塞耳机2代咖啡金色照片,不过仔细观察小米正品黑色手环会发现最令人小米惊叹的是一小米正品活塞耳机3代黑色只正在经过的飞鸟,被十分清晰地抓拍了下来,没有丝毫模糊,要知道手机拍摄动态物体的实际成像效果一般都不怎么理小米正品活塞耳机2代咖啡金色想的,更何况是一只正在飞翔的大鸟,画面并没有出现抖动情况,很清晰,由此可见小米5在运动抓拍方面应该很小米给力。&#39;;
$tags = array(array(&#39;tagName&#39; => &#39;小米正品活塞耳机2代咖啡金色&#39;,&#39;tagUrl&#39; => &#39;http://www.mi.com/kfj/&#39;), array(&#39;tagName&#39; => &#39;小米正品活塞耳机3代黑色&#39;,&#39;tagUrl&#39; => &#39;http://www.mi.com/hei/&#39;), array(&#39;tagName&#39; => &#39;小米&#39;,&#39;tagUrl&#39; => &#39;http://www.mi.com/&#39;));
$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;
?>
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!