Jump to content

BBcode


zero_ZX

Recommended Posts

Hi,

Any one got some tips on this? I found a pecl package, but it seemed very difficult, that I had to install different stuff. I wonder that when I use (for example) SMF, bbcodes just work, without installing anything on my server, so what are those guys using? I would really appreciate if I didn't have to install anything.

Link to comment
Share on other sites

regex will be your new best friend.

ill give you a push in the right direction.

 

$string = '[b]this[/b] is [b]a[/b] very [b]boldy[/b] message.
[url=http://www.google.ca/]click here[/url] or this [url]http://www.google.ca/[/url]';

$bbc = array(
'b'=>array(
	'expression'=>'/\[b\](.*?)\[\/b\]/',
	'result'=>'<span style=font-weight:bold;>\\1</span>',
),
'url2'=>array(
	'name'=>'Link',
	'display_in_commands'=>false,
	'expression'=>'/\[url=(.*?)\](.*?)\[\/url\]/',
	'result'=>'<a href="\\1" target="_blank">\\2</a>',
),
'url'=>array(
	'name'=>'Link',
	'display_in_commands'=>true,
	'expression'=>'/\[url\](.*?)\[\/url\]/',
	'result'=>'<a href="\\1" target="_blank">\\1</a>',
)
);

$expressions = $results = array();
foreach($bbc as $tag => $code){
$expressions[] = $code['expression'];
$results[] = $code['result'];
}
$string = preg_replace($expressions,$results,$string);
$string = str_replace("\r\n",'<br />',$string);//convert line breaks.

echo $string;

 

you should be able to extend with more tags now, enjoy :)

Link to comment
Share on other sites

PECL is a library of PHP extensions written in C, which are compiled into your PHP installation. Forum software like SMF provides their own PHP-based implementations. There is a PEAR package (BBCodeParser) you can use to handle this though, or plenty of examples online if you don't want to write your own. If you want to have a bash at it though, as RobertP says you'll need to use regular expressions.

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.