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
https://forums.phpfreaks.com/topic/202944-code-bbcode/
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
https://forums.phpfreaks.com/topic/202944-code-bbcode/#findComment-1063497
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.