thunderbox Posted July 1, 2007 Share Posted July 1, 2007 hey, im making a website, its not a forum or anything like that but it does require the user of BBcode. i decided to use this because allowing the users to use html (like how myspace did) creates too many security risks and i like my design how it is so i am going to keep it anyways. i want to know how i can implement BBcode on my page. i need all the basic functions. does anyone have any areas of expertise here. ive looked at multiple tutorials but i get lost in them all . does anyone have a link to a good on? thanx Quote Link to comment https://forums.phpfreaks.com/topic/57922-php-bbcode-parser/ Share on other sites More sharing options...
pocobueno1388 Posted July 1, 2007 Share Posted July 1, 2007 all you need to do is use the function str_replace() http://php.net/str_replace That way you can display the users input from the database and replace any BBcode with HTML tags. Say this is what the user submitted (and is what is stored in the database): [b]Hello![/b] My name is .... You could replace the and with HTML tags, like this: <?php $text = "[b]Hello![/b] My name is ...."; echo "<b>Before</b><br> $text<p>"; $search = array('[b]', '[/b]'); //add as many tags as you need $replace = array('<b>', '</b>'); $text = str_replace($search, $replace, $text); echo "<b>After</b><br> $text"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/57922-php-bbcode-parser/#findComment-287011 Share on other sites More sharing options...
corbin Posted July 1, 2007 Share Posted July 1, 2007 Poco, can I ask about what you would do with: [url]http://google.com/[/url] You could use regular expressions to handle things like that... $content = '[url=http://google.com]http://google.com[/url]'; $pattern = '/\[url\](.*)\[\/url\]/'; $replacement = '<a href="\\1" target="_blank">link</a>'; $content = preg_replace($pattern, $replacement, $content); Quote Link to comment https://forums.phpfreaks.com/topic/57922-php-bbcode-parser/#findComment-287028 Share on other sites More sharing options...
pocobueno1388 Posted July 1, 2007 Share Posted July 1, 2007 Quote Poco, can I ask about what you would do with: [url]http://google.com/[/url] You could use regular expressions to handle things like that... $content = '[url=http://google.com]http://google.com[/url]'; $pattern = '/\[url\](.*)\[\/url\]/'; $replacement = '<a href="\\1" target="_blank">link</a>'; $content = preg_replace($pattern, $replacement, $content); Well...you basically just answered the question =P I would have probably been pretty stumped on that one though, I am terrible with regular expressions. Quote Link to comment https://forums.phpfreaks.com/topic/57922-php-bbcode-parser/#findComment-287031 Share on other sites More sharing options...
thunderbox Posted July 1, 2007 Author Share Posted July 1, 2007 thanx guys. ill see if i can get it to work later tonight! Quote Link to comment https://forums.phpfreaks.com/topic/57922-php-bbcode-parser/#findComment-287048 Share on other sites More sharing options...
thunderbox Posted July 1, 2007 Author Share Posted July 1, 2007 it worked thanks guys. however i want to apply this to 2 fields in my form, not just one. (About and Links). what is the easiest way to implement this. or do i jsut copy the function twice? Quote Link to comment https://forums.phpfreaks.com/topic/57922-php-bbcode-parser/#findComment-287069 Share on other sites More sharing options...
pocobueno1388 Posted July 1, 2007 Share Posted July 1, 2007 Just make the code into a function: <?php function bbcode($str){ $search = array('[b]', '[/b]'); $replace = array('<b>', '</b>'); $str = str_replace($search, $replace, $str); return $str; } ?> To use it, just call it like this: <?php bbcode($text); ?> Quote Link to comment https://forums.phpfreaks.com/topic/57922-php-bbcode-parser/#findComment-287242 Share on other sites More sharing options...
LuAn Posted July 1, 2007 Share Posted July 1, 2007 The solution posted by pocobueno1388 is fine if you can trust your users to use a closing tag every time they use an opening tag. Oh wow! A pig just flew by my window. ;-) Better to use a regex solution, like the one suggested by corbin, even for simple tags like bold tags. That way you won't convert an opening BBCode tag to html unless there is a corresponding closing tag. Quote Link to comment https://forums.phpfreaks.com/topic/57922-php-bbcode-parser/#findComment-287268 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.