bbcode解析时遇见的两个问题!

WBOY
Release: 2016-06-23 13:48:03
Original
946 people have browsed it

网上一直没找到合适的bbcode解析类
所以东抄西抄的做了一个
但是出现两问题了,请各位高手协助一下。

代码在最下

问题一:bbcode解析时遇到[code][/code]不做任何处理...比如表情/其他bbcode等不转义

问题二:如何过滤HTML?
我用BBCODE保存资料就是不想用户用HTML.....

但我测试时...发现如何手动输入:

<div style="color:red">dddd</div>
Copy after login


竟然能真的有效


所以如果是插入一些js或者iframe应该也是可以的吧?

有什么方法可方止?

下面提供整个class 代码, 刚开始, 东抄西抄组装,还没优化好,请见谅


<?phpclass BBCode {      public function __construct(){}    private function showBBcodes($text) {        // BBcode array        $find = array(            '~\[b\](.*?)\[/b\]~s',            '~\[i\](.*?)\[/i\]~s',            '~\[u\](.*?)\[/u\]~s',            '~\[quote\](.*?)\[/quote\]~s',            '~\[table\](.*?)\[/table\]~s',            '~\[tr\](.*?)\[/tr\]~s',            '~\[td\](.*?)\[/td\]~s',            '~\[justify\](.*?)\[/justify\]~s',            '~\[center\](.*?)\[/center\]~s',            '~\[right\](.*?)\[/right\]~s',            '~\[left\](.*?)\[/left\]~s',            '~\[size=(.*?)\](.*?)\[/size\]~s',            '~\[color=(.*?)\](.*?)\[/color\]~s',            '~\[font=(.*?)\](.*?)\[/font\]~s',            '~\[url=((?:ftp|https?)://.*?)\](.*?)\[/url\]~s',            '~\[img\](https?://.*?\.(?:jpg|jpeg|gif|png|bmp))\[/img\]~s',            '/\[img=(\d+)x(\d+)\](.*?)\[\/img\]/is',            '~\[youtube\](.*?)\[/youtube\]~s',        );        // HTML tags to replace BBcode        $replace = array(            '<b>$1</b>',            '<i>$1</i>',            '<span style="text-decoration:underline;">$1</span>',            '<pre class="brush:php;toolbar:false">$1</'.'pre>',            '<table>$1</table>',            '<tr>$1</tr>',            '<td>$1</td>',            '<div align="justify">$1</div>',            '<div align="center">$1</div>',            '<div align="right">$1</div>',            '<div align="left">$1</div>',            '<span class="h$1">$2</span>',            '<span style="color:$1;">$2</span>',            '<span style="font:$1;">$2</span>',            '<a href="$1" target="_blank" rel="nofollow">$2</a>',            '<img src="$1" alt=""/>',            '<img width="$1" height="$2" src="$3" alt="" />',            '<iframe width="560"    style="max-width:90%" src="http://www.youtube.com/embed/$1?wmode=opaque" data-youtube-id="$1" frameborder="0" allowfullscreen=""></iframe>'        );        return nl2br(preg_replace($find,$replace,$text));    }    //表情转义    private function parseSmiley($text){        // Smiley to image        $smileys = array(            ':wave:' => 'wave.gif',            ':hahaha:' => 'hahaha.gif',            ':hahahau:' => 'hahahau.gif',            ':help:' => 'help.gif'        );        // Now you need find and replace        foreach($smileys as $smiley => $img){            $text = str_replace(                    $smiley,                "<img src='{$img}' alt='{$smiley}'/>",                $text            );        }        // Now only return it        return $text;        }    //为连结自动加上A标签    private function linkAdd($content){        //提取替换出所有A标签(统一标记<{link}>)        preg_match_all('/<a.*?href=".*?".*?>.*?<\/a>/i',$content,$linkList);        $linkList=$linkList[0];        $str=preg_replace('/<a.*?href=".*?".*?>.*?<\/a>/i','<{link}>',$content);        //提取替换出所有的IMG标签(统一标记<{img}>)        preg_match_all('/<img [^ alt="bbcode解析时遇见的两个问题!" >]+>/im',$content,$imgList);        $imgList=$imgList[0];        $str=preg_replace('/<img [^ alt="bbcode解析时遇见的两个问题!" >]+>/im','<{img}>',$str);        //提取替换出所有的YOUTUBE标签(统一标记<{img}>)        preg_match_all('/<iframe[^>]+>/im',$content,$youtubeList);        $youtubeList=$youtubeList[0];        $str=preg_replace('/<iframe[^>]+>/im','<{iframe}>',$str);        //提取替换标准的URL地址        $str=preg_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_/+.~#?&//=]+)','<a href="\\0" target="_blank" rel="nofollow">\\0</a>',$str);        //还原A统一标记为原来的A标签        $arrLen=count($linkList);        for($i=0;$i<$arrLen;$i++){            $str=preg_replace('/<{link}>/',$linkList[$i],$str,1);         }                //还原IMG统一标记为原来的IMG标签        $arrLen2=count($imgList);        for($i=0;$i<$arrLen2;$i++){            $str=preg_replace('/<{img}>/',$imgList[$i],$str,1);         }        //还原IMG统一标记为原来的YOUTUBE标签        $arrLen2=count($youtubeList);        for($i=0;$i<$arrLen2;$i++){            $str=preg_replace('/<{iframe}>/',$youtubeList[$i],$str,1);         }        return $str;    }    public function parser($message){        $parser_content = $message;        $parser_content = $this->showBBcodes($parser_content);        $parser_content = $this->linkAdd($parser_content);        $parser_content = $this->parseSmiley($parser_content);        return $parser_content;    }}
Copy after login


回复讨论(解决方案)

private function showBBcodes($text) {
$text = htmlspecialchars($text); //编码已存在的 HTML
preg_match_all('#\[code\](.*?)\[/code]#is', $text, $stack);

//原来的数组赋值

$text = nl2br(preg_replace($find,$replace,$text));
foreach($stack[1] as $t) {
$text = preg_replsce('#\[code\].*?\[/code]#is', $t, 1);
}
return $text;
}

大哥,谢谢提醒
竟然忘了 htmlspecialchars


但有关[code]

           $text = preg_replace('#\[code\].*?\
Copy after login
Copy after login
#is', $t, 1); [/code]
这样不知为什么不行,只要整篇内容有包含 [code]...就整篇内容变成 "1"

后来改成 :
           $text = preg_replace('#\[code\].*?\
Copy after login
Copy after login
#is', $t, $text); [/code]

改这样
有没有什么大问题吗?

$text = preg_replsce('#\[code\].*?\[/code]#is', $t,  $text, 1); 
不好意思,写漏了

不知为什么出不了代码
本代大哥你提供的是

           $text = preg_replace('#\[code\].*?\[/code]#is', $t, 1); 


这样不知为什么不行,只要整篇内容有包含 [code]...就整篇内容变成 "1" 

要改成
           $text = preg_replace('#\[code\].*?\[/code]#is', $t, $text); 

才能用

请教下这样会有大问题吗?

$text = preg_replsce('#\[code\].*?\[/code]#is', $t,  $text, 1); 
不好意思,写漏了

原来这样
明白
太感谢了
Related labels:
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!