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
https://forums.phpfreaks.com/topic/257869-bbcode/
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
https://forums.phpfreaks.com/topic/257869-bbcode/#findComment-1321741
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
https://forums.phpfreaks.com/topic/257869-bbcode/#findComment-1321782
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.