zero_ZX Posted February 27, 2012 Share Posted February 27, 2012 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 More sharing options...
RobertP Posted February 27, 2012 Share Posted February 27, 2012 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 More sharing options...
Adam Posted February 27, 2012 Share Posted February 27, 2012 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 More sharing options...
zero_ZX Posted February 28, 2012 Author Share Posted February 28, 2012 Thanks for the replies.. I found this library, and it was very usefull. I'm sure yours works as well, this was a bit faster though http://nbbc.sourceforge.net/ Link to comment https://forums.phpfreaks.com/topic/257869-bbcode/#findComment-1321964 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.