Jump to content

How to Safely Quote Someone?


doubledee

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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;
    }

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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?!  :P

 

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

 

 

Link to comment
Share on other sites

And how does that happen?  Gremlins?!  :P

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

 

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.