BBCode the two problems encountered when parsing!

Source: Internet
Author: User
Tags bbcode
I've never found a suitable BBCode parsing class on the Internet.
So the east copy of the West to do a
But there are two problems, please help a master.

Code at the bottom

Issue One: BBCode parsing encountered [Code][/code] do not do any processing ... such as expression/other BBCode not escaped

question two: How to filter HTML?
I use BBCode to save the data is not want users to use HTML ...

But when I was testing ... Discover how to manually enter:
dddd


It actually works.


So if it is to insert some JS or iframe should also be OK?

Is there any way to stop?

The following provides the entire class code, just beginning, East copy of the Assembly, not optimized, please forgive me.


 $',            ' $',            ' $',            '
$
  ',            '
  
  
   
   $
  
  
', '$', '$', ' $ ', ' $ ', ' $ ', ' $ ', ' $', ' $', ' $', ' the ' $ ', ', ', ', '); Return nl2br (Preg_replace ($find, $replace, $text)); }//Expression escape 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, "", $text); }//Now only return it return $text; }//For the link Auto plus a tag private function Linkadd ($content) {//extract replace all a label (Unified Mark <{link}>) Preg_match_all ('/. *?<\/a>/i ', $content, $linkList); $linkList = $linkList [0]; $str =preg_replace ('/.*?<\/a>/i ', ' <{link}> ', $content); Extract replaces all IMG tags (unified tagging <{img}>) Preg_match_All ('/]+>/im ', $content, $imgList); $imgList = $imgList [0]; $str =preg_replace ('/]+>/im ', ' <{img}> ', $str); Extract replaces all YouTube tags (unified tagging <{img}>) Preg_match_all ('/ ]+>/im ', $content, $youtubeList); $youtubeList = $youtubeList [0]; $str =preg_replace ('/ ]+>/im ', ' <{iframe}> ', $str); Extract the URL address of the replacement standard $str =preg_replace (' ((f|ht) {1}tp://) [-a-za-z0-9@:%_/+.~#?&//=]+] ', ' \\0 ', $str); Restore a unified mark as the original a tag $arrLen =count ($linkList); for ($i =0; $i < $arrLen; $i + +) {$str =preg_replace ('/<{link}>/', $linkList [$i], $STR, 1); }//Restore IMG Unified tagged as the original img tag $arrLen 2=count ($imgList); for ($i =0; $i < $arrLen 2; $i + +) {$str =preg_replace ('/<{img}>/', $imgList [$i], $STR, 1); }//Restore IMG Unified tagged as original youtube tag $arrLen 2=count ($youtubeList); for ($i =0; $i < $arrLen 2; $i + +) {$str =preg_replace ('/<{iframe}>/', $youtubeList [$i], $STR, 1); } return $STR; The 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; }}


Reply to discussion (solution)

Private Function Showbbcodes ($text) {
$text = Htmlspecialchars ($text); Encode an existing HTML
Preg_match_all (' #\[code\] (. *?) \[/code] #is ', $text, $stack);

The original array assignment

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

Brother, thank you for reminding me
I forgot htmlspecialchars.


But about [code]

           $text = Preg_replace (' #\[code\].*?\
#is ', $t, 1); [/code]
So I do not know why not, as long as the entire content contains [code] ... "1" for the whole story.

Later changed to:
           $text = Preg_replace (' #\[code\].*?\
#is ', $t, $text); [/code]

change this.
Do you have any big questions
?

$text = Preg_replsce (' #\[code\].*?\[/code] #is ', $t, $text, 1);
Sorry, I missed it.

I don't know why I can't get the code.
Brother Ben, what you're offering is

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


So I do not know why not, as long as the entire content contains [code] ... "1" for the whole story.

To change into
$text = Preg_replace (' #\[code\].*?\[/code] #is ', $t, $text);

To use

Would it be a big problem to ask?

$text = Preg_replsce (' #\[code\].*?\[/code] #is ', $t, $text, 1);
Sorry, I missed it.

It turns out.
Got it
Thank you so much.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.