Jump to content

Recommended Posts

i have made so many attempts at creating a bbcode system... i cant stand it.

something where my users can put no! in there blog post, and it shows as bold...

or a [link=www.google.com]THERE CUSTOM TEXT HERE[/link] to make there own links...

italics, underline, marque... mostly just the basic stuff...

 

 

i just dont get how any of it could be done!!! anyone? :shrug:

 

Link to comment
https://forums.phpfreaks.com/topic/184601-bbcode-what/
Share on other sites

The basic concept is not at all complicated. You allow the user to input some text data to represent the start and end of where they want to apply the formatting. So for example, if they want bold they just type. Please make this word bold. Since has no meaning in HTML your system needs to convert it to something meaningfull before outputing it. Since in HTML the <b> tags make something bold you just replace and with <b> and </b> tags respectively. You can use the exact same technique for italic, underline and any other tag that uses on/off tags. There's nothing to stop these being done with str_replace or similar because there is no complicated matching involved. URL's are a little more complex since they need to match a pattern, hence why Regex is required. A very basic example of how it is done. You allow the user to input Google and then substitute it for <a href="http://www.google.com">Google</a>. An example of how this can be done (in it's most simplest form, it's probably not perfect...

 

$pattern = '#\[url=http://([^\]]+)\](.+?)\[/url\]#';
$input = "This is a link to [url=http://www.google.com]Google[/url].";
$input = preg_replace($pattern, '<a href="$1">$2</a>', $input);[/url]

Link to comment
https://forums.phpfreaks.com/topic/184601-bbcode-what/#findComment-974878
Share on other sites

Like I said, probably not perfect... ;) that's what you get for rushing I guess. You could obviously just remove the http:// part from the actual pattern causing it to be captured by the following capture group (it would of course require that the user entered it).

 

[ot]The really bizarre thing is that I noticed that when I posted it and I thought I edited it. I was having trouble with my Internet yesterday though, so perhaps it just didn't post the edit.  :-\ [/ot]

Link to comment
https://forums.phpfreaks.com/topic/184601-bbcode-what/#findComment-975322
Share on other sites

No, and apparently you don't understand. You stop the user inputting HTML, so they instead use BBCode. So lets say the user enters

 

I'm testing to see just how good your <b>script</b> is.

 

As a security feature your script would probably either use strip_tags to get rid of the two <b> tags or alternatively use htmlentities to convert them to harmless characters (as this forum obviously does else you wouldn't have seen them in my post). At that point there's two options, your script will either store the string into the database (which is probably what I'd do) and convert the BBCode to HTML as part of the display script. Or at that point you could convert the BBCode to HTML and store it in that format in your database.

Link to comment
https://forums.phpfreaks.com/topic/184601-bbcode-what/#findComment-975504
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.