Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Wrap a string in double quotes

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

like image 775
Rowan Avatar asked Oct 12 '09 16:10

Rowan


4 Answers

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.

like image 79
timmow Avatar answered Nov 12 '22 13:11

timmow


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.

like image 35
Brian Schroth Avatar answered Nov 12 '22 14:11

Brian Schroth


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.

like image 2
David Thomas Avatar answered Nov 12 '22 14:11

David Thomas


I did it this way:

function quotize(&$string)
{
    if (empty($string) || is_null($string)) {
        return;
    }

    $char = '"';
    if ($string[0] != $char) {
        $string = $char . $string;
    }

    if (substr($string, -1) != $char) {
        $string .= $char; 
    }

    return;
}
like image 1
pro100tom Avatar answered Nov 12 '22 13:11

pro100tom