marksie1988 Posted January 13, 2008 Share Posted January 13, 2008 i have created a bbcode for my website and here it is <? function bbcode($txt){ $txt = preg_replace_callback('#\[php\](.+?)\[/php\]#msi',create_function('$matches', 'return highlight_string($matches[1], true);'), $txt); $txt = preg_replace('#\[b\](.+?)\[/b\]#msi', '<b>\\1</b>', $txt); $txt = preg_replace('#\[b\](.+?)\[/b\]#msi', '<b>\\1</b>', $txt); $txt = preg_replace('#\[i\](.+?)\[/i\]#msi', '<i>\\1</i>', $txt); $txt = preg_replace('#\[i\](.+?)\[/i\]#msi', '<i>\\1</i>', $txt); $txt = preg_replace('#\[u\](.+?)\[/u\]#msi', '<u>\\1</u>', $txt); $txt = preg_replace('#\[u\](.+?)\[/u\]#msi', '<u>\\1</u>', $txt); $txt = preg_replace('#\[s\](.+?)\[/s\]#msi', '<s>\\1</s>', $txt); $txt = preg_replace('#\[s\](.+?)\[/s\]#msi', '<s>\\1</s>', $txt); //email $txt = preg_replace('#\[email\](.+?)\[/email\]#msi', '<a href="mailto:\1">\\1</a>', $txt); //code $txt = preg_replace('#\[code\](.+?)\[/code\]#msi', '<div style="border: solid 1px orange; padding:20px; margin: 20px">\\1</div>', $txt); //images $txt = preg_replace('#\[img\](.+?)\[/img\]#msi', '<img src="\\1" border=0>', $txt); //smilies $txt = str_replace('','<img src="../../images/smilies/smile2.gif">',$txt); $txt = str_replace('','<img src="../../images/smilies/sad.gif">',$txt); $txt = str_replace(':$','<img src="../../images/smilies/confoosed.gif">',$txt); $txt = str_replace(':teeth:','<img src="../../images/smilies/bigteeth.gif">',$txt); $txt = str_replace(':cooldude:','<img src="../../images/smilies/cooldude.gif">',$txt); $txt = str_replace(':cglasses:','<img src="../../images/smilies/coolglasses.gif">',$txt); $txt = str_replace('3:>','<img src="../../images/smilies/devil.gif">',$txt); $txt = str_replace('','<img src="../../images/smilies/embaressed.gif">',$txt); $txt = str_replace('','<img src="../../images/smilies/geek.gif">',$txt); $txt = str_replace(':evillook:','<img src="../../images/smilies/grr.gif">',$txt); $txt = str_replace(':hoh:','<img src="../../images/smilies/oh.gif">',$txt); $txt = str_replace('','<img src="../../images/smilies/pirate.gif">',$txt); $txt = str_replace(':robot:','<img src="../../images/smilies/robot.gif">',$txt); $txt = str_replace(':robot:','<img src="../../images/smilies/skull.gif">',$txt); $txt = str_replace(':]','<img src="../../images/smilies/smile.gif">',$txt); $txt = str_replace(':|','<img src="../../images/smilies/straight.gif">',$txt); $txt = str_replace(':stumped:','<img src="../../images/smilies/thinking.gif">',$txt); $txt = str_replace('','<img src="../../images/smilies/tongue.gif">',$txt); $txt = str_replace(':vamp:','<img src="../../images/smilies/vampire.gif">',$txt); $txt = str_replace(':7','<img src="../../images/smilies/vface.gif">',$txt); $txt = str_replace(':cat:','<img src="../../images/smilies/wisky.gif">',$txt); $txt = str_replace('','<img src="../../images/smilies/winky.gif">',$txt); return $txt; } ?> the problem im having is that i dont know how to set it so that when someone has a new line in the textarea that it adds a <br /> also when i use the code box it still executes everything within it as if it is bbcode or scripts. i dont know how to stop this from happening anyone know ? Thanks Steve Quote Link to comment https://forums.phpfreaks.com/topic/85875-solved-bbcode-help/ Share on other sites More sharing options...
phpSensei Posted January 13, 2008 Share Posted January 13, 2008 i have created a bbcode for my website and here it is <? function bbcode($txt){ $txt = preg_replace_callback('#\[php\](.+?)\[/php\]#msi',create_function('$matches', 'return highlight_string($matches[1], true);'), $txt); $txt = preg_replace('#\[b\](.+?)\[/b\]#msi', '<b>\\1</b>', $txt); $txt = preg_replace('#\[b\](.+?)\[/b\]#msi', '<b>\\1</b>', $txt); $txt = preg_replace('#\[i\](.+?)\[/i\]#msi', '<i>\\1</i>', $txt); $txt = preg_replace('#\[i\](.+?)\[/i\]#msi', '<i>\\1</i>', $txt); $txt = preg_replace('#\[u\](.+?)\[/u\]#msi', '<u>\\1</u>', $txt); $txt = preg_replace('#\[u\](.+?)\[/u\]#msi', '<u>\\1</u>', $txt); $txt = preg_replace('#\[s\](.+?)\[/s\]#msi', '<s>\\1</s>', $txt); $txt = preg_replace('#\[s\](.+?)\[/s\]#msi', '<s>\\1</s>', $txt); //email $txt = preg_replace('#\[email\](.+?)\[/email\]#msi', '<a href="mailto:\1">\\1</a>', $txt); //code $txt = preg_replace('#\[code\](.+?)\[/code\]#msi', '<div style="border: solid 1px orange; padding:20px; margin: 20px">\\1</div>', $txt); //images $txt = preg_replace('#\[img\](.+?)\[/img\]#msi', '<img src="\\1" border=0>', $txt); //smilies $txt = str_replace('','<img src="../../images/smilies/smile2.gif">',$txt); $txt = str_replace('','<img src="../../images/smilies/sad.gif">',$txt); $txt = str_replace(':$','<img src="../../images/smilies/confoosed.gif">',$txt); $txt = str_replace(':teeth:','<img src="../../images/smilies/bigteeth.gif">',$txt); $txt = str_replace(':cooldude:','<img src="../../images/smilies/cooldude.gif">',$txt); $txt = str_replace(':cglasses:','<img src="../../images/smilies/coolglasses.gif">',$txt); $txt = str_replace('3:>','<img src="../../images/smilies/devil.gif">',$txt); $txt = str_replace('','<img src="../../images/smilies/embaressed.gif">',$txt); $txt = str_replace('','<img src="../../images/smilies/geek.gif">',$txt); $txt = str_replace(':evillook:','<img src="../../images/smilies/grr.gif">',$txt); $txt = str_replace(':hoh:','<img src="../../images/smilies/oh.gif">',$txt); $txt = str_replace('','<img src="../../images/smilies/pirate.gif">',$txt); $txt = str_replace(':robot:','<img src="../../images/smilies/robot.gif">',$txt); $txt = str_replace(':robot:','<img src="../../images/smilies/skull.gif">',$txt); $txt = str_replace(':]','<img src="../../images/smilies/smile.gif">',$txt); $txt = str_replace(':|','<img src="../../images/smilies/straight.gif">',$txt); $txt = str_replace(':stumped:','<img src="../../images/smilies/thinking.gif">',$txt); $txt = str_replace('','<img src="../../images/smilies/tongue.gif">',$txt); $txt = str_replace(':vamp:','<img src="../../images/smilies/vampire.gif">',$txt); $txt = str_replace(':7','<img src="../../images/smilies/vface.gif">',$txt); $txt = str_replace(':cat:','<img src="../../images/smilies/wisky.gif">',$txt); $txt = str_replace('','<img src="../../images/smilies/winky.gif">',$txt); return $txt; } ?> the problem im having is that i dont know how to set it so that when someone has a new line in the textarea that it adds a < br /> also when i use the code box it still executes everything within it as if it is bbcode or scripts. i dont know how to stop this from happening anyone know ? Thanks Steve For the < br > you use a simple function like nl2br($text) Quote Link to comment https://forums.phpfreaks.com/topic/85875-solved-bbcode-help/#findComment-438406 Share on other sites More sharing options...
marksie1988 Posted January 13, 2008 Author Share Posted January 13, 2008 ok that worked thanks just need the answer to how i can stop the code from displaying any chars as bbcode i want anything inside them tage to do the same as they do on this forum Quote Link to comment https://forums.phpfreaks.com/topic/85875-solved-bbcode-help/#findComment-438410 Share on other sites More sharing options...
monkeytooth Posted January 13, 2008 Share Posted January 13, 2008 instead of preg_replace try str_replace Quote Link to comment https://forums.phpfreaks.com/topic/85875-solved-bbcode-help/#findComment-438412 Share on other sites More sharing options...
marksie1988 Posted January 13, 2008 Author Share Posted January 13, 2008 instead of preg_replace try str_replace that doesnt work still shows the str_replace smilies in it. Quote Link to comment https://forums.phpfreaks.com/topic/85875-solved-bbcode-help/#findComment-438423 Share on other sites More sharing options...
phpSensei Posted January 13, 2008 Share Posted January 13, 2008 What is your problem now? I don't get it... <?php include("bbcode.php"); $txt = $_POST['text']; $txt = bbcode($txt); echo $txt; ?> Quote Link to comment https://forums.phpfreaks.com/topic/85875-solved-bbcode-help/#findComment-438428 Share on other sites More sharing options...
marksie1988 Posted January 13, 2008 Author Share Posted January 13, 2008 look here http://www.blconline.co.uk/tutorials/tutorial.blc?id=4 if you look inside the first code box all of the bbcode tags are showing as bbcode when i dont want them to i want anything inside the code tags to simply show as text nothing else Quote Link to comment https://forums.phpfreaks.com/topic/85875-solved-bbcode-help/#findComment-438430 Share on other sites More sharing options...
phpSensei Posted January 14, 2008 Share Posted January 14, 2008 make the image code "<img src = " Quote Link to comment https://forums.phpfreaks.com/topic/85875-solved-bbcode-help/#findComment-438433 Share on other sites More sharing options...
marksie1988 Posted January 14, 2008 Author Share Posted January 14, 2008 make the image code "<img src = " but if i do that wont it stop all of the bb code from working ? Quote Link to comment https://forums.phpfreaks.com/topic/85875-solved-bbcode-help/#findComment-438435 Share on other sites More sharing options...
phpSensei Posted January 14, 2008 Share Posted January 14, 2008 make the image code "<img src = " but if i do that wont it stop all of the bb code from working ? no $txt = str_replace('','<img src="../../images/smilies/sad.gif">',$txt); Quote Link to comment https://forums.phpfreaks.com/topic/85875-solved-bbcode-help/#findComment-438436 Share on other sites More sharing options...
monkeytooth Posted January 14, 2008 Share Posted January 14, 2008 since this code is for display from what I gather by the link given.. I am assuming that that is a iframe or div tag.. either way.. why not <plaintext>insert code to display here</plaintext> Quote Link to comment https://forums.phpfreaks.com/topic/85875-solved-bbcode-help/#findComment-438441 Share on other sites More sharing options...
marksie1988 Posted January 14, 2008 Author Share Posted January 14, 2008 since this code is for display from what I gather by the link given.. I am assuming that that is a iframe or div tag.. either way.. why not <plaintext>insert code to display here</plaintext> monkeytooth yours is the closest to what i need but unfortunatly the plaintext tag never closes i looked it up and doesnt look like you can close that tag so i need one along those lines. i dont know how smf and other forums do it but surley doing it the same way as them would be easiest Quote Link to comment https://forums.phpfreaks.com/topic/85875-solved-bbcode-help/#findComment-438450 Share on other sites More sharing options...
redarrow Posted January 14, 2008 Share Posted January 14, 2008 <?php function bbcode_format ($str) { $str = htmlentities($str); $simple_search = array( //added line break '/\[br\]/is', '/\[b\](.*?)\[\/b\]/is', '/\[i\](.*?)\[\/i\]/is', '/\[u\](.*?)\[\/u\]/is', '/\[url\=(.*?)\](.*?)\[\/url\]/is', '/\[url\](.*?)\[\/url\]/is', '/\[align\=(left|center|right)\](.*?)\[\/align\]/is', '/\[img\](.*?)\[\/img\]/is', '/\[mail\=(.*?)\](.*?)\[\/mail\]/is', '/\[mail\](.*?)\[\/mail\]/is', '/\[font\=(.*?)\](.*?)\[\/font\]/is', '/\[size\=(.*?)\](.*?)\[\/size\]/is', '/\[color\=(.*?)\](.*?)\[\/color\]/is', //added textarea for code presentation '/\[codearea\](.*?)\[\/codearea\]/is', //added pre class for code presentation '/\[code\](.*?)\[\/code\]/is', //added paragraph '/\[p\](.*?)\[\/p\]/is', ); $simple_replace = array( //added line break '<br />', '<strong>$1</strong>', '<em>$1</em>', '<u>$1</u>', // added nofollow to prevent spam '<a href="$1" rel="nofollow" title="$2 - $1">$2</a>', '<a href="$1" rel="nofollow" title="$1">$1</a>', '<div style="text-align: $1;">$2</div>', //added alt attribute for validation '<img src="$1" alt="" />', '<a href="mailto:$1">$2</a>', '<a href="mailto:$1">$1</a>', '<span style="font-family: $1;">$2</span>', '<span style="font-size: $1;">$2</span>', '<span style="color: $1;">$2</span>', //added textarea for code presentation '<textarea class="code_container" rows="30" cols="70">$1</textarea>', //added pre class for code presentation '<pre class="code">$1</pre>', //added paragraph '<p>$1</p>', ); // Do simple BBCode's $str = preg_replace ($simple_search, $simple_replace, $str); // Do <blockquote> BBCode $str = bbcode_quote ($str); return $str; } function bbcode_quote ($str) { //added div and class for quotes $open = '<blockquote><div class="quote">'; $close = '</div></blockquote>'; // How often is the open tag? preg_match_all ('/\[quote\]/i', $str, $matches); $opentags = count($matches['0']); // How often is the close tag? preg_match_all ('/\[\/quote\]/i', $str, $matches); $closetags = count($matches['0']); // Check how many tags have been unclosed // And add the unclosing tag at the end of the message $unclosed = $opentags - $closetags; for ($i = 0; $i < $unclosed; $i++) { $str .= '</div></blockquote>'; } // Do replacement $str = str_replace ('[' . 'quote]', $open, $str); $str = str_replace ('[/' . 'quote]', $close, $str); return $str; } /* used in Vision.To CMS function VISION_TO_PAGE_CONTENT_PROCESSOR ($content) { $content=bbcode_format ($content); return $content; } */ /*Usage in CodeCharge Studio : before show event , the content_html is label property as HTML $content=bbcode_format ($cms_pages->content_html->GetValue()); $cms_pages->content_html->SetValue($content); */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/85875-solved-bbcode-help/#findComment-438456 Share on other sites More sharing options...
marksie1988 Posted January 14, 2008 Author Share Posted January 14, 2008 thanks for that one it works perfectly thanks again steve Quote Link to comment https://forums.phpfreaks.com/topic/85875-solved-bbcode-help/#findComment-438473 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.