doubledee Posted March 15, 2012 Share Posted March 15, 2012 I just finished adding the ability to add Comments after Articles on my website. Now it would be nice if people could Quote other Posts/Comments juts like you can do here on PHPFreaks. The problem is that I have this code to eliminate any security issues with HTML... echo ' <div class="userPost"> <span class="commentDate">Posted on: ' . date('Y-m-d g:ia', strtotime($createdOn)) . '</span> <span class="commentNo">#' . $commentCount . '</span> <p>' . nl2br(htmlentities($comments, ENT_QUOTES)) . '</p> </div>'; Any suggestions to have it both ways? Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/ Share on other sites More sharing options...
ManiacDan Posted March 15, 2012 Share Posted March 15, 2012 The generally accepted way to do this is to use bbcode or wikimarkup or some other non-html formatting language to generate the quotes. Reddit just considers any line that begins with a right angle bracket to be a quote. PHPFreaks obviously uses bbcode [ quote=thePerson ] tags (with a LOT of extra data). BBCode parsers are difficult to write but should be easy to find online. Reddit-style highlighting indicators and/or @tags would be easier. Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327540 Share on other sites More sharing options...
doubledee Posted March 15, 2012 Author Share Posted March 15, 2012 The generally accepted way to do this is to use bbcode or wikimarkup or some other non-html formatting language to generate the quotes. What do I have to do to use bbcode? Write something myself? Install something? How hard is that? How important do you think it is for Users to be able to quote one another? (The context of my question is this... My website is full of Articles and I now allow Users to post Comments on the Articles. So it is sorta like PHPFreaks, but for focused on the Articles verus a User-selected topic.) Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327547 Share on other sites More sharing options...
darkfreaks Posted March 15, 2012 Share Posted March 15, 2012 http://www.christian-seiler.de/projekte/php/bbcode/index_en.html this is the link to the BBcode class you can use to to add [Quote] feature to your comment section and here is a working example on how to do so. http://stackoverflow.com/questions/1344857/quoting-users-in-forums Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327549 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 This could probably be refined, but it will get you on the right track. function parse_bbcode($data) { // make sure open quote tags match closed quote tags $open_quote = substr_count($data, '[quote]'); $closed_quote = substr_count($data, '[/quote]'); if ($open_quote > $closed_quote) { $diff = $open_quote - $closed_quote; // add closed tags for($i = 0; $i < $diff; $i++) { $data .= '[/quote]'; } } else if ($closed_quote > $open_quote) { $diff = $closed_quote - $open_quote; // add open tags for($i = 0; $i < $diff; $i++) { $data .= '[quote]'; } } $search = array( '/\[quote\]/i', '/\[\/quote\]/i' ); $replace = array( '<blockquote>', '</blockquote>' ); return preg_replace($search, $replace, $data); } Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327661 Share on other sites More sharing options...
Psycho Posted March 15, 2012 Share Posted March 15, 2012 @scootstah: A couple critiques: 1) If open quotes need to be added, I think they should be added to the beginning of the string. 2) Instead of using a for() loop to add the opening/closing tags, you could just use str_repeat() if ($open_quote > $closed_quote) { //Add needed closing tags to end of string $data .= str_repeat('[\quote]', $open_quote - $closed_quote); } else if ($closed_quote > $open_quote) { //Add needed opening tags to beginning of string $data = str_repeat('[quote]', $closed_quote - $open_quote) . $data; } Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327682 Share on other sites More sharing options...
doubledee Posted March 15, 2012 Author Share Posted March 15, 2012 scootstah, I'm not following what you are trying to do. Could explain in English first, please? Thanks. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327683 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 1) If open quotes need to be added, I think they should be added to the beginning of the string. Yeah, I guess that makes more sense. 2) Instead of using a for() loop to add the opening/closing tags, you could just use str_repeat() Also makes sense. Sometimes I forget that PHP pretty much has a function for everything. scootstah, I'm not following what you are trying to do. Could explain in English first, please? I made a function that parses BBCode and turns it into HTML. You write quotes just like you do on these forums, and then the function will turn it into HTML. At this point it only handles quotes, but you could easily add any other BBCode syntax for things like bold, italics, underline, etc. Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327739 Share on other sites More sharing options...
doubledee Posted March 15, 2012 Author Share Posted March 15, 2012 I made a function that parses BBCode and turns it into HTML. You write quotes just like you do on these forums, and then the function will turn it into HTML. At this point it only handles quotes, but you could easily add any other BBCode syntax for things like bold, italics, underline, etc. Rewind for a second. Is BBCode a "language" or is it an "application" or is it an "add-on"? From my limited research, it sounds like PHP has some BBCode functionality built into it? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327750 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 http://en.wikipedia.org/wiki/BBCode First Google result. There is a PECL package for BBCode. http://www.php.net/manual/en/book.bbcode.php Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327755 Share on other sites More sharing options...
doubledee Posted March 15, 2012 Author Share Posted March 15, 2012 http://en.wikipedia.org/wiki/BBCode First Google result. There is a PECL package for BBCode. http://www.php.net/manual/en/book.bbcode.php I already read those. So how do I know if BBCode is supported by my web host? And if it is supported, what do I have to do to start using BBCode? It says it is a "Markup Language", but I'm a little unclear how that relates to PHP. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327793 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 It has nothing to do with your host, or with PHP. It is simply text that you convert into HTML. [ b] becomes <b>, [ i] becomes <i>, etc. Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327794 Share on other sites More sharing options...
doubledee Posted March 15, 2012 Author Share Posted March 15, 2012 It has nothing to do with your host, or with PHP. It is simply text that you convert into HTML. [ b] becomes <b>, [ i] becomes <i>, etc. And how does that happen? Gremlins?! I would suspect that I need some BBCode "add on" to my base PHP installation. And I was asking WHAT that would be and WHERE would I look to see if I have it? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327804 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 And how does that happen? Gremlins?! Had you bothered to read the code I posted earlier, it should be pretty obvious. You simply replace the BBCode with HTML. You don't need anything installed on your server, it is not an addon. It is replacing one piece of text with another piece of text. $bbcode = '[b]'; $html = str_replace('[b]', '<b>', $bbcode); I don't know of a simpler way to explain it. Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327807 Share on other sites More sharing options...
ManiacDan Posted March 15, 2012 Share Posted March 15, 2012 There is no built-in base bbcode library you can compile into your PHP installation. Edit: I meant that you probably don't have it already compiled and we know you run on shared hosting /Edit. There are only custom-written functions like the one you were already handed. Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327825 Share on other sites More sharing options...
KevinM1 Posted March 15, 2012 Share Posted March 15, 2012 BBCode is a standard text formatting language that you need to figure out how to implement. The standard essentially just spells out what the tags (like , for example) should do. External libraries, like PECL, have code that you can use in your projects to do most of the heavy lifting. There are also implementations listed on the BBCode site itself: http://www.bbcode.org/implementations.php That said, look at the different standards before making the jump. I actually like Markdown better than BBCode because it's faster to specify formatting while typing (it's what Stack Overflow uses). The downside is that it's less known/more esoteric than BBCode. You'll need to balance ease of use, ease of implementation, user expectations, etc. Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327826 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 There is no built-in base bbcode library you can compile into your PHP installation. There are only custom-written functions like the one you were already handed. Well there's this. I've never used it so I don't know how well it works. EDIT: I actually like Markdown better than BBCode because it's faster to specify formatting while typing (it's what Stack Overflow uses). I was about to flip complete shit trying to figure out how to properly post code on SO until I figured out you can just hit "ctrl+k" to automatically format it. Other sites (like reddit) unfortunately do not share that, so posting code still sucks. Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327827 Share on other sites More sharing options...
KevinM1 Posted March 15, 2012 Share Posted March 15, 2012 EDIT: I actually like Markdown better than BBCode because it's faster to specify formatting while typing (it's what Stack Overflow uses). I was about to flip complete shit trying to figure out how to properly post code on SO until I figured out you can just hit "ctrl+k" to automatically format it. Other sites (like reddit) unfortunately do not share that, so posting code still sucks. Huh... If you have 4+ space tabs, all you need to do is copy/paste the code over, and tag your message with the language you're posting about. Code formatting starts after the 4th space. EDIT: For SO... Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327834 Share on other sites More sharing options...
doubledee Posted March 15, 2012 Author Share Posted March 15, 2012 There is no built-in base bbcode library you can compile into your PHP installation. This makes it sound like you have to install something... http://php.net/manual/en/book.bbcode.php Edit: I meant that you probably don't have it already compiled and we know you run on shared hosting /Edit. We do? Well, you are wrong... There are only custom-written functions like the one you were already handed. From what I see on Google it looks like there may be an application that translates from BBCodes to HTML, but unfortunately I shut off my laptop and lost where I was at. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327887 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 That PECL package does exactly the same thing as any other bbcode library. I already gave you exactly what you asked for. All you have to do is copy and paste it. Quote Link to comment https://forums.phpfreaks.com/topic/258967-how-to-safely-quote-someone/#findComment-1327908 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.