Bbcode resolution [code] problem This post was last edited by sky94132003 from 2014-10-3016: 41: 28. you have asked related questions. the moderator has initially solved bbs. csdn. nettopics390900798 but later found... it is impossible to add a css to the code, such as the following bbcode parsing [code] problem.
This post was last edited by sky94132003 at 16:41:28
I have asked related questions before.
The moderator has initially solved the problem.
Http://bbs.csdn.net/topics/390900798
But later I found that it is impossible to add a css to the code.
For example, the following code
The [code] is filtered out in showBBcodes ().
However, if you add code to the array to display the code and want to convert it to the specified DIV..., then there will be a problem!
Question 2: How can I filter the [code] of all functions at a time? For example, parseSmiley () and linkAdd ()
Is there a more intelligent way? Or can I add them to every one?
Code makeup, but because there is a [code] in the code, there is no need to post the code function. sorry.
The full code is here: click me
Class BBCode {
Public function _ construct (){}
Private function showBBcodes ($ text ){
$ Text = htmlspecialchars ($ text); // Encode the existing HTML
Preg_match_all ('# \ [code \] (. *?) \ [/Code] # is ', $ text, $ stack );
// BBcode array
$ Find = array (
'~ \ [B \] (. *?) \ [/B \] ~ S ',
'~ \ [I \] (. *?) \ [/I \] ~ S ',
'~ \ [Quote \] (. *?) \ [/Quote \] ~ S ',
'~ \ [Code \] (. *?) \ [/Code \] ~ S'
);
// HTML tags to replace BBcode
$ Replace = array (
'
$1',
'
$1',
'
$1
',
'
$1
'
);
$ Text = nl2br (preg_replace ($ find, $ replace, $ text ));
Foreach ($ stack [1] as $ t ){
$ Text = preg_replace ('# \ [code \]. *? \ [/Code] # is ', $ t, $ text, 1 );
}
Return $ text;
}
// Escape expression
Private function parseSmiley ($ text ){
// Smiley to image
$ Smileys = array (
': Wave:' => 'wave.gif ',
': Hahaha:' => 'hahaha.gif ',
);
// Now you need find and replace
Foreach ($ smileys as $ smiley => $ img ){
$ Text = str_replace (
$ Smiley,
"",
$ Text
);
}
// Now only return it
Return $ text;
}
// Automatically add A label to the link
Private function linkAdd ($ content ){
// Extract and replace all A Tags (unified tag <{link}>)
Preg_match_all ('/.*? <\/A>/I ', $ content, $ linkList );
$ LinkList = $ linkList [0];
$ Str = preg_replace ('/.*? <\/A>/I ',' <{link}> ', $ content );
// Extract and replace all IMG tags (unified tag <{img}>)
Preg_match_all ('/] +>/im', $ content, $ imgList );
$ ImgList = $ imgList [0];
$ Str = preg_replace ('/] +>/im', '<{img}>', $ str );
// Extract and replace all YOUTUBE tags (unified tag <{img}>)
Preg_match_all ('/ ] +>/Im ', $ content, $ youtubeList );
$ YoutubeList = $ youtubeList [0];
$ Str = preg_replace ('/ ] +>/Im ',' <{iframe}> ', $ str );
// Extract and replace the standard URL address
$ Str = preg_replace ('(f | ht) {1} tp: //) [-a-zA-Z0-9 @: % _/+ .~ #? & // =] +) ',' \ 0', $ str );
// Restore the uniform A tag to the original A tag
$ ArrLen = count ($ linkList );
For ($ I = 0; $ I <$ arrLen; $ I ++ ){
$ Str = preg_replace ('/<{link}>/', $ linkList [$ I], $ str, 1 );
}
// Restore the IMG tag to the original IMG tag
$ ArrLen2 = count ($ imgList );
For ($ I = 0; $ I <$ arrLen2; $ I ++ ){
$ Str = preg_replace ('/<{img}>/', $ imgList [$ I], $ str, 1 );
}
// Restore IMG to mark it as the original YOUTUBE tag
$ ArrLen2 = count ($ youtubeList );
For ($ I = 0; $ I <$ arrLen2; $ I ++ ){
$ Str = preg_replace ('/<{iframe}>/', $ youtubeList [$ I], $ str, 1 );
}
Return $ str;
}
Function noparse ($ text = null ){
$ Text = str_replace (array ('[', ']'), array ('* NoParse1 *', '* NoParse2 *'), $ text );
Return $ text;
}
Public function parser ($ message ){
$ Parser_content = $ message;
$ Parser_content = $ this-> showBBcodes ($ parser_content );
$ Parser_content = $ this-> parseSmiley ($ parser_content );
$ Parser_content = $ this-> linkAdd ($ parser_content );
Return $ parser_content;
}
}
------ Solution ----------------------
Please provide the test text and expected results