phpfan101 Posted December 10, 2009 Share Posted December 10, 2009 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? Quote Link to comment Share on other sites More sharing options...
trq Posted December 10, 2009 Share Posted December 10, 2009 There are plenty of already existing classes capable of this, have you tried googling for a solution? Quote Link to comment Share on other sites More sharing options...
phpfan101 Posted December 10, 2009 Author Share Posted December 10, 2009 Yes, i have. And yes, it did show results. the problem is i just don't understand any, i was hoping here i could get a better grasp on what it all means, i'm fairly new to php Quote Link to comment Share on other sites More sharing options...
cags Posted December 10, 2009 Share Posted December 10, 2009 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] Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 11, 2009 Share Posted December 11, 2009 Actually, that will not work, cags. Using yours, Google will become <a href="www.google.com">Google</a> (it's missing the schema part). Quote Link to comment Share on other sites More sharing options...
cags Posted December 11, 2009 Share Posted December 11, 2009 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] Quote Link to comment Share on other sites More sharing options...
phpfan101 Posted December 11, 2009 Author Share Posted December 11, 2009 yes, i understand what you mean, but i have all html blocked, so a user cannot post any... So, if i set it up like that, when a user goes ahead and try's it, then wont it be stopped by the html blocker i have set up for posts??? Quote Link to comment Share on other sites More sharing options...
cags Posted December 11, 2009 Share Posted December 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
phpfan101 Posted December 11, 2009 Author Share Posted December 11, 2009 oh, wow.... I Didnt even think of just converting it like that, after the post is striped of tags sorry about that, thanks!! Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 11, 2009 Share Posted December 11, 2009 You wouldn't want to strip the tags. What if the user wants to talk about HTML like in this topic? Quote Link to comment Share on other sites More sharing options...
phpfan101 Posted December 12, 2009 Author Share Posted December 12, 2009 I have different sections for my forum, most is on the topic of the site, when nothing along the lines of that should be discussed.... I did add an off-topic section with most enabled though Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 12, 2009 Share Posted December 12, 2009 You're missing my point. You generally shouldn't mess with the post. Just escape it using htmlentities. It's equally safe and it preserves the content. Quote Link to comment Share on other sites More sharing options...
emopoops Posted December 12, 2009 Share Posted December 12, 2009 yeah just use the replace function replace everything with [ Quote Link to comment 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.