Jump to content

[SOLVED] bbcode help


marksie1988

Recommended Posts

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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);  
*/  
?>  

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.