views:

671

answers:

4

I'm attempting to wrap a user-inputted string in double quotes for output but I want to make sure I don't end up with double double quotes on either side of the string. For example, if a user posts

"Hello"

I don't want to turn it into

""Hello""

I can do this fairly easily using the code below, however I'm concerned that this may get slow if I'm looping through lots of strings.

$string = '"'.trim($string,'"').'"';

If anyone has a better way of doing this, that'd be great. Equally, if anybody can confirm that my way is fine, I'll be happy.

Thanks

+4  A: 

Hi,

This is exactly how I would solve this problem. It's only worth worrying about the code being slow if you have a problem with the application being slow, and you can trace it down to the trim statements.

A well known programming quote is "Premature Optimisation is the root of all evil" - see the wikipedia article linked for more on this.

timmow
Good article, I'll give it a read. Thanks
Rowan
+2  A: 

Personally, I'd suggest stripping the quotes on input. As for making it faster, if you allow quotes to be entered or stored, you're always going to be stuck with using an if/else before displaying them.

Obviously, you'd still need to perform a sanity check of the input data regardless of whatever system you end up with.

David Thomas
thanks for your answer, I agree but the system i'm working on was put together partially by somebody else, so I have to work with horrible existing data.. I will eventually sanitize it all!
Rowan
A: 

It is common practice to use quotation marks to represent a quote within a quote, if you want to let your users write:
and she said "hey ho"

, which you could turn into:
User: "and she said 'hey ho'"

chelmertz
+1  A: 

Make a careful consideration of what should happen with all the cases. (using [] as quotes for readability)

You've said what you do for ["Hello"] but what do you do for [I said "Hello", punk]? Do you still strip the user-input quotes, or do you remove them? Or maybe go one step further and substitute single quotes for double quotes...but then you'd have to consider the cases where the user input contains both single and double quotes! What about when the user puts in "grammatically wrong" text like ["Hello] (no closing quote!).

Best way to ensure you do it right is to make a test case for each edge case you can think of and make sure your proposed solution actually does what is expected.

If the actually reason for this requirement is [I am taking a user's input at some point and then re-displaying it to them at another point, and don't want to show them stupid looking data like [""data""]], you'll probably just want to only remove double quotes from the start or end of the input string, because stripping them from the middle screws with the user's intended data.

If your goal is merely to clearly distinguish between their input and text that they didn't input, consider using other means of highlighting that instead of quotation marks. So instead of [you entered "data"], you could display [you entered data] or [you entered: data] which avoid this problem altogether.

Brian Schroth
+1 for use-case tests, a good point that I'm ashamed to have missed in my own answer... =/
David Thomas