Jump to content

[code] BBCode


Tazerenix

Recommended Posts

Hey.

My problem is, im working on a bbcode class. I've got everything working, all the normal BBCodes, quotes even. The only problem i'm having is with the code BBCode.

I can get it to partially work but if someone puts a /code as part of the inside of a code section it ends the section. I'm not entirely sure how to stop it. Anyone know how?

Link to comment
Share on other sites

<?php

class BBCode
{
function htmlout($text)
{
	$html = array(
		'~\<~' => '<',
		'~\>~' => '>'
	);
	$text = preg_replace(array_keys($html), $html, $text);
	return $text;
}

function lines($text)
{
	$parts = preg_split('~(\[/code\]|\[code(?:=[^\]]+)?\])~i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
	for ($x = 0, $y = count($parts); $x < $y; $x++)
        {
            if ($x % 4 == 0)
            {
			$parts2 = preg_split('~(\[/quote\]|\[quote(?:=[^\]]+)?\])~i', $parts[$x], -1, PREG_SPLIT_DELIM_CAPTURE);
			for ($a = 0, $b = count($parts2); $a < $b; $a++)
			{
				if ($a % 4 == 0)
				{
					$parts2[$a] = nl2br($parts2[$a]);
				}
			}
		}
	}
	$text = implode('', $parts);
	return $text;
}

function code($text)
{
	$text = preg_replace('~\[code\]~is', "\n" . '<br /><h3 class="bbcode_code">Code:</h3>' . "\n" . '<pre class="brush: plain">', $text);
	$text = preg_replace('~\[code="(.*)"\]~i', "\n" . '<br /><h3 class="bbcode_code">Code: ($1)</h3>' . "\n" . '<pre class="brush: $1">', $text);
	$text = preg_replace('~\[/code\]~is', '</pre>' . "\n", $text);
	return $text;
}

function quotes($text)
{
	$parts = preg_split('~(\[/code\]|\[code(?:=[^\]]+)?\])~i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
	for ($x = 0, $y = count($parts); $x < $y; $x++)
	{
		if ($x % 4 == 0)
		{
			$parts[$x] = preg_replace('~\[/quote\]~is', '</div>' . "\n", $parts[$x]);
			$parts[$x] = preg_replace('~\[quote="(.*)"\]~i', "\n" . '<div class="bbcode_quote">' . "\n" . '<h3 class="bbcode_quote">$1 said:</h3>' . "\n", $parts[$x]);
			$parts[$x] = preg_replace('~\[quote\]~is', "\n" . '<div class="bbcode_quote">' . "\n" . '<h3 class="bbcode_quote">Quote:</h3>' . "\n", $parts[$x]);
		}
	}
	$text = implode('', $parts);
	return $text;
}

    function parse($text)
    {		
	// Build an array of common bbcodes
	$bbcodes = array(
		'~\[b\](.*)\[/b\]~is' 						=> '<span class="bbcode_b">$1</span>',
		'~\[u\](.*)\[/u\]~is' 						=> '<span class="bbcode_u">$1</span>',
		'~\[i\](.*)\[/i\]~is' 						=> '<span class="bbcode_i">$1</span>',
		'~\[s\](.*)\[/s\]~is' 						=> '<span class="bbcode_s">$1</span>',
		'~\[url\](.*)\[/url\]~is'					=> '<a href="$1">$1</a>',
		'~\[url=(.*)\](.*)\[/url\]~is'				=> '<a href="$1">$2</a>',
		'~\[img\](.*)\[/img\]~is' 					=> '<img src="$1" class="bbcode_img" />',
		'~\[email\](.*)\[/email\]~is' 				=> '<a href="mailto:$1">$1</a>',
		'~\[email=(.*)\](.*)\[/email\]~is' 			=> '<a href="mailto:$1">$2</a>',
		'~\[size=(.{0,3})\](.*)\[/size\]~is'		=> '<span class="bbcode_size" style="font-size:$1%">$2</span>',
		'~\[color=(.*)\](.*)\[/color\]~is'			=> '<span style="color:$1">$2</span>'					
        );

        $parts_1 = preg_split('~(\[/code\]|\[code(?:=[^\]]+)?\])~i', $text, -1, PREG_SPLIT_DELIM_CAPTURE); // Split into parts depending on [code]
        for ($x = 0, $y = count($parts_1); $x < $y; $x++)
        {
            if ($x % 4 == 0)
            {
			$parts_1[$x] = preg_replace(array_keys($bbcodes), $bbcodes, $parts_1[$x]);
            }
        }
        $parts_1 = implode('', $parts_1);
	$parts_1 = $this->lines($parts_1);

	$parts_1 = $this->code($parts_1);
	$parts_1 = $this->quotes($parts_1);

        return $parts_1;
    }
    
    function preparse($text)
    {
	$text = strtr($text, array("\r" => '')); // get rid of carrage return
	$text = $this->htmlout($text);

	$quoteopen = preg_match_all('~(\[quote(?:=[^\]]+)?\])~is', $text, $dummy);
	$quoteclose = preg_match_all('~(\[/quote\])~is', $text, $dummy);
	if ($quoteopen > $quoteclose)
		$text .= str_repeat("\n" . '[/quote]', $quoteopen - $quoteclose);
	else if ($quoteclose > $quoteopen)
		$text = str_repeat('[quote]', $quoteclose - $quoteopen) . $text;

	/* Close/Open [code] tags */
	$codeopen = preg_match_all('~(\[code(?:=[^\]]+)?\])~is', $text, $dummy);
	$codeclose = preg_match_all('~(\[/code\])~is', $text, $dummy);
	if ($codeopen > $codeclose)
		$text .= str_repeat("\n" . '

', $codeopen - $codeclose);

else if ($codeclose > $codeopen)

$text = str_repeat('

', $codeclose - $codeopen) . $text;

	return $text;
}
}

?>

 

Thats my current BBCode Class

(please don't pwn me for it being shit, i'm not the greatest at php).

 

My current method, preparses the data (before entering a database). And then, when the bbcode is parsed, it just replaces each code it finds. I have also tried a regexp method but it didnt really go to well. (i actually deleted that, sorry).

 

Any ideas?[/code]

Link to comment
Share on other sites

Your point?

 

phpBB likely allows


blocks to appear raw when there is no data within them. I still fail to see the logic in your question / problem. What exactly would you expect your parser to do when it sees a closing [/code] tag? I would expect it to close the code block.

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.