Tazerenix Posted May 26, 2010 Share Posted May 26, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/202944-code-bbcode/ Share on other sites More sharing options...
JAY6390 Posted May 26, 2010 Share Posted May 26, 2010 Can you show an example of what you have that works and what you have that doesn't Quote Link to comment https://forums.phpfreaks.com/topic/202944-code-bbcode/#findComment-1063493 Share on other sites More sharing options...
Tazerenix Posted May 26, 2010 Author Share Posted May 26, 2010 <?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] Quote Link to comment https://forums.phpfreaks.com/topic/202944-code-bbcode/#findComment-1063497 Share on other sites More sharing options...
trq Posted May 26, 2010 Share Posted May 26, 2010 but if someone puts a /code as part of the inside of a code section it ends the section  What exactly would you expect to happen? Any parser still needs the correct syntax given to it. Quote Link to comment https://forums.phpfreaks.com/topic/202944-code-bbcode/#findComment-1063499 Share on other sites More sharing options...
JAY6390 Posted May 26, 2010 Share Posted May 26, 2010 Very true. You can't parse a [/code] within a block. Those are the basic principles the parser works on Quote Link to comment https://forums.phpfreaks.com/topic/202944-code-bbcode/#findComment-1063503 Share on other sites More sharing options...
Tazerenix Posted May 26, 2010 Author Share Posted May 26, 2010 strange, phpbb's one seems to do it Quote Link to comment https://forums.phpfreaks.com/topic/202944-code-bbcode/#findComment-1063506 Share on other sites More sharing options...
JAY6390 Posted May 26, 2010 Share Posted May 26, 2010 You can put [/code] within a block and it doesn't cut off straight away? Quote Link to comment https://forums.phpfreaks.com/topic/202944-code-bbcode/#findComment-1063509 Share on other sites More sharing options...
Tazerenix Posted May 26, 2010 Author Share Posted May 26, 2010 mhmm, http://eobots-online.info/viewtopic.php?f=2&t=42&sid=08e6189e70443c0ddd330e96e2929412 Quote Link to comment https://forums.phpfreaks.com/topic/202944-code-bbcode/#findComment-1063515 Share on other sites More sharing options...
trq Posted May 26, 2010 Share Posted May 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/202944-code-bbcode/#findComment-1063517 Share on other sites More sharing options...
Tazerenix Posted May 26, 2010 Author Share Posted May 26, 2010 yea yea, i guess. XD. Well thanks for that. Now i can continue working without feeling dumb for it doing that lol Quote Link to comment https://forums.phpfreaks.com/topic/202944-code-bbcode/#findComment-1063519 Share on other sites More sharing options...
ChristianeGuise Posted May 31, 2010 Share Posted May 31, 2010 sorry to intrude I just would like to know if you received my message and if you could help me you seem so good at PHP Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/202944-code-bbcode/#findComment-1065555 Share on other sites More sharing options...
Tazerenix Posted May 31, 2010 Author Share Posted May 31, 2010 who? i don't think i got any messages lately Quote Link to comment https://forums.phpfreaks.com/topic/202944-code-bbcode/#findComment-1065601 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.