Jump to content

Bbcode function issue


pandailo

Recommended Posts

Hi all,

 

Actually i manage to create bbcode function but there is a problem especially with user accidentally mistype the bbcode

For example, if user type the bbcode like this test, it will give an parse error. How do i solve the issue

 

Here is my bbcode function

 

function getbbcode($text, $ses="", $filtered)

{

  $text = htmlspecialchars($text);

  $text=preg_replace("/\[b\](.*?)\[\/b\]/i","<b>\\1</b>", $text);

  $text=preg_replace("/\[b\](.*?)\[\/u\]/i","<b>\\1</b>", $text);

  $text=preg_replace("/\[i\](.*?)\[\/i\]/i","<i>\\1</i>", $text);

  $text=preg_replace("/\[u\](.*?)\[\/u\]/i","<u>\\1</u>", $text);

  $text=preg_replace("/\[u\](.*?)\[\/b\]/i","<u>\\1</u>", $text);

  $text=preg_replace("/\[big\](.*?)\[\/big\]/i","<big>\\1</big>", $text);

  $text=preg_replace("/\[small\](.*?)\[\/small\]/i","<small>\\1</small>", $text);

  return $text;

}

 

I tried belows way to solve the issue:

$text=preg_replace("/\[b\][u\](.*?)\[\/b\][\/u\]/i","<b>\\1</b>", $text);

$text=preg_replace("/\[b\][u\](.*?)\[\/b\]\[\/u\]/i","<b>\\1</b>", $text);

$text=preg_replace("/\[b\]/\[u\](.*?)\[\/b\]\[\/u\]/i","<b>\\1</b>", $text);

 

but none is working. Anybody can solve the issue?

 

Link to comment
Share on other sites

For BBCode, try this instead, it's what I use

 

$Find = array
(
'/\[b\]/is',
'/\[\/b\]/is',
'/\[i\]/is',
'/\[\/i\]/is'
'/\[u\]/is',
'/\[\/u\]/is',
'/\[big\]/is',
'/\[\/big\]/is',
'/\[small\]/is',
'/\[\/small\]/is'
);

$Replace = array
(
'<b>',
'</b>',
'<i>',
'</i>',
'<u>',
'</u>',
'<big>',
'</big>',
'<small>',
'</small>'
);

$Text = preg_replace($Search, $Replace, $Text);

 

Your problem was it replaced [xx]aa[/xx] with <xx>aa</xx>. You didn't say what to do if the [/xx] wasn't matched with anything. My code tells it to replace, no matter what

Link to comment
Share on other sites

im using this one

$txt = str_replace("[b]", "<b>", $txt);
$txt = str_replace("[/b]", "</b>", $txt);
$txt = str_replace("[i]", "<i>", $txt);
$txt = str_replace("[/i]", "</i>", $txt);
$txt = str_replace("[u]", "<u>", $txt);
$txt = str_replace("[/u]", "</u>", $txt);
$txt = str_replace("[quote]", "<div id=quote><b>QUOTE:</b>", $txt);
$txt = str_replace("[/quote]", "</div>", $txt);
$txt = preg_replace("#\[color=(.+?)\](.+?)\[/color\]#is","<font color=\"\\1\">\\2</font>",$txt);

Link to comment
Share on other sites

Nah, I wrote up what you need, here it is

 

<?php

function BBCode($Text)
{
$Find = array
	(
	'/\[b\]/is',
	'/\[\/b\]/is',
	'/\[i\]/is',
	'/\[\/i\]/is',
	'/\[u\]/is',
	'/\[\/u\]/is',
	'/\[big\]/is',
	'/\[\/big\]/is',
	'/\[small\]/is',
	'/\[\/small\]/is'
	);

$Replace = array
	(
	'<b>',
	'</b>',
	'<i>',
	'</i>',
	'<u>',
	'</u>',
	'<big>',
	'</big>',
	'<small>',
	'</small>'
	);

$Text = preg_replace($Find, $Replace, $Text);
return $Text;
}

$Text = "[u][b]Your message here[/u][/b]";

echo BBCode($Text);

?>

Link to comment
Share on other sites

i know what y'all trying to help but all your function is working bbcode..no problem to me also...my above function also 4 working bbcode.....my problem still remain unsolve because what i explain in this topic is mistype bbcode just as show in the example above...and to prove the mistype bbcode is working prefectly is in this forum itself. If i type

 

[b ][u ]test[ /b][ /u]  --> see the result test

 

that is what i want, when user mistype the bbcode, the function automatically detect bold and underline bbcode like example above.

Link to comment
Share on other sites

i will give you what i use right now , should be good enough no problem at those "errors"

 

function bb($bbcode){
     $bbcode = htmlentities($bbcode);
     $searchFor = array(
 '/\[b\](.*?)\[\/b\]/is', 
 '/\[i\](.*?)\[\/i\]/is', 
 '/\[url\=(.*?)\](.*?)\[\/url\]/is'
 );
     $replaceWith = array(
 '<b>$1</b>',
 '<i>$1</i>',
 '<a href="$1">$2</a>'
 );
     $html = preg_replace($searchFor, $replaceWith, $bbcode);
     return $html;
}

 

 

//edit

 

now saw -.-

why do you use [ b] and [ /b] in different arrays? * no space lol*

Link to comment
Share on other sites

this will require much more complex code. It is always best to parse BBCode tags in pairs and never on a singular basis. That way if users mistype a tag or place a tag where it shouldn't be your layout wont screw up.

 

You should look into adding a "Preview" area so the user can check whatever they are about to post looks correctly and apply any amendments before posting their message.

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.