Prismatic Posted July 9, 2006 Share Posted July 9, 2006 Working on my forums again and I need a foolproof method of getting code (any kind, if it's code, it can go) transformed into a safe version to be processed and regurgatated back when someone views a post.For instance, I have a [bbcode][/bbcode] tag on my forums which, when you use it, produces a code block very similar to the forums here currently (Actually identical, I really liked your code blocks :P ). I need a way to make all code "safe" in that it wont be run when I want to show it back, it also has to be able to pass through preg_replace without causing any errors (it likes to fail evaluation on some characters)Any help is appreciated :) Quote Link to comment https://forums.phpfreaks.com/topic/14074-whats-a-foolproof-method-to-allow-code-posting/ Share on other sites More sharing options...
kenrbnsn Posted July 9, 2006 Share Posted July 9, 2006 Use the function htmlentities() on any string you display back to the screen.What do mean by:[quote]t also has to be able to pass through preg_replace without causing any errors (it likes to fail evaluation on some characters)[/quote]Ken Quote Link to comment https://forums.phpfreaks.com/topic/14074-whats-a-foolproof-method-to-allow-code-posting/#findComment-55027 Share on other sites More sharing options...
Daniel0 Posted July 9, 2006 Share Posted July 9, 2006 [quote author=kenrbnsn link=topic=99929.msg393836#msg393836 date=1152416357]What do mean by:[quote]t also has to be able to pass through preg_replace without causing any errors (it likes to fail evaluation on some characters)[/quote][/quote]I think he is using it for his BBCodes.Here is an example of the [b]bold[/b], [i]italic[/i] and [u]underline[/u] BBCodes: [code]$t = preg_replace("`\[b\](.*)\[/b\]`sUi","<b>\\1</b>",$t);$t = preg_replace("`\[i\](.*)\[/i\]`sUi","<i>\\1</i>",$t);$t = preg_replace("`\[u\](.*)\[/u\]`sUi","<u>\\1</u>",$t);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14074-whats-a-foolproof-method-to-allow-code-posting/#findComment-55038 Share on other sites More sharing options...
Prismatic Posted July 9, 2006 Author Share Posted July 9, 2006 I dont need all BB codes, I already have those done. I mean accepting php, html, all those kinds of code and processing them so I can safely store them in the database without the fear of them getting run and causing issues. Quote Link to comment https://forums.phpfreaks.com/topic/14074-whats-a-foolproof-method-to-allow-code-posting/#findComment-55046 Share on other sites More sharing options...
Daniel0 Posted July 9, 2006 Share Posted July 9, 2006 Ahh, just run the through htmlentities() as Ken said ;) Quote Link to comment https://forums.phpfreaks.com/topic/14074-whats-a-foolproof-method-to-allow-code-posting/#findComment-55047 Share on other sites More sharing options...
kenrbnsn Posted July 9, 2006 Share Posted July 9, 2006 Also, always process all text with mysql_real_escape_string() when inserting/updating the text into the database.Ken Quote Link to comment https://forums.phpfreaks.com/topic/14074-whats-a-foolproof-method-to-allow-code-posting/#findComment-55079 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.