I've never found a suitable BBCode parsing class on the Internet.
But there are two problems, please help a master.
I use BBCode to save the data is not want users to use HTML ...
But when I was testing ... Discover how to manually enter:
It actually works.
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.