Jump to content

PHP BBcode Parser


thunderbox

Recommended Posts

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

Link to comment
Share on other sites

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";

?>

 

 

 

 

 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

 

Link to comment
Share on other sites

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.

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.