Error with self coded BBCode system's [code] tag.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Aristona
Forum Commoner
Posts: 33
Joined: Thu Dec 02, 2010 8:14 am

Error with self coded BBCode system's [code] tag.

Post by Aristona »

Hello,

I was coding a BBCode system of my own but recently I am having a problem I cannot get pass through.

The code is this.

[syntax=php]
<?php
class BBCode
{
function GetStringBetween($string, $start, $end)
{
$string = " ". $string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}

function ReadBBCode($text)
{
$text = preg_replace("(\[b\](.+?)\[\/b\])is", '<b>$1</b>', $text); // Bold
$text = preg_replace("(\[i\](.+?)\[\/i\])is", '<i>$1</i>', $text); // Italic
$text = highlight_string($this->GetStringBetween($text, '[code]', '[/code]')); // Italic
return $text;
}
}
$text = '[b]BOLD[/b]
[code]<?php echo "aaa"; ?>[/code]
[code]<?php echo "aaa"; ?>[/code]
[code]<?php echo "aaa"; ?>[/code]
[code]<?php echo "aaa"; ?>[/code]
[code]<?php echo "aaa"; ?>[/code]';

$site = new BBCode();
echo $site->ReadBBCode($text);
?>
[/syntax]

I know the code isn't that great but I did so many tweaks on it since I couldn't get [code] syntax to work. I know the usage of this code, but I am stuck here.

This code works like, it automatically replaces [ b ]text[ / b ] with < b >text < / b > so the text is being bold. However, when I need to use code, it should show the codes. Right now PHP recognise them as code and give me "aaa" output. I solved it by doing a htmlspecialchars, but then again, whole string is being "text only". String doesn't even recognise nl2br commands.

What I want to do is;

$string
[text]
Basic Text
(B)Bold text(/B)
(I)Italic Text(/I)
(Code)PHP Code here(/Code)
(Code)PHP Code 2 here(/Code)
(Code)PHP Code 3 here(/Code)
[/text]

Output should be;

[text]
Basic Text
[b]Bold Text[/b] --> This is bold.
[i]Italic Text[/i] ---> This is italic.
(Code)PHP Code here(/Code) --> This is where I am stuck. It will show this as "aaa",
(Code)PHP Code 2 here(/Code) --> If I solve it by selecting text between codes as a string, it will only recognise first code tags. This line won't be effected.
(Code)PHP Code 3 here(/Code) ---> Same goes for this.
[/text]

As said, if I use htmlspecialchars, whole string is being text only so it doesn't recognise <b> commands either. If I take string between [code] tags and highlight them with highlight_code, it only shows the first [code] tag.

I think I should be doing something like; (algorithm)
foreach [$string between '[ code ]' and '[ /code ]]
$string = highlight_string($string);
return $string;

^ Something like this, but as said, I am stuck and confused as hell.
Any help will be greatly appreciated.

Thank you.

Ps. I hate using other BBCode scripts in my website, so please don't reply me with "Give up if you cannot even do this." comments. I want to code my own BBCode system, even if I fail, I want to improve my knowledge by trying.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Error with self coded BBCode system's [code] tag.

Post by Celauran »

The problem I see here is that GetStringBetween returns the text between [code] and [/code], discarding everything before and after it. That being so, you'd need to isolate the section(s) containing [code][/code], send those to the function separately, then work them back into your original string.
Post Reply