例子
評論表情使用的標籤<emt>1</emt>至<emt>60</emt>。
對應的圖片路徑/Public/images/face/1.gif至/Public/images/face/60.gif。
實現方法是使用preg_replace函數完成評論表情的顯示。
<?php
$message='文章寫的太好了<emt>3</emt><emt>34</emt>';
$message=preg_replace('#<emt>(\d{1,2})</emt>#', '<img src="/Public/images/face/${1}.gif" />', $message);
echo $message;
?>
例子
1.把html元素全部去掉,或者保留某幾個html標籤
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "/n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
結果為(去掉了注釋):
<blockquote>Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a></blockquote>2.相反,只去掉某一個html標籤
<?php
function strip_only($str, $tags, $stripContent = false) {
$content = '';
if(!is_array($tags)) {
$tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
if(end($tags) == '') array_pop($tags);
}
foreach($tags as $tag) {
if ($stripContent)
$content = '(.+</'.$tag.'[^>]*>|)';
$str = preg_replace('#</?'.$tag.'[^>]*>'.$content.'#is', '', $str);
}
return $str;
}
$str = '<font color="red">red</font> text';
$tags = 'font';
$a = strip_only($str, $tags); // red text
$b = strip_only($str, $tags, true); // text
?>