1

Firstly which of these functions return a string with single quote:

$data = strval($data);     
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = mysqli_real_escape_string($mysqli,$data);

And how can I assure my string has double quotes and both ends instead of ' ? Tyvm for your help

EDIT: I receive my string directly in a POST (as in $received = $_POST["string"]) and run $received through one of those functions above that ultimately turns it into '$received' since it does not react to explode("\n",$received)

Fane
  • 1,978
  • 8
  • 30
  • 58
  • I sense a XY problem. What are you trying to do? – Federkun Dec 05 '15 at 16:16
  • 3
    A string does not have single or double quotes by default. They are only there if you add them to the string. Maybe it would help if you would clearly state what you are planning to do. – maxhb Dec 05 '15 at 16:18
  • @Federico updated question – Fane Dec 05 '15 at 16:26
  • @maxhb updated question – Fane Dec 05 '15 at 16:26
  • I think you are overthinking, string is just string. single or double quote is made by us – Andrew Dec 05 '15 at 16:38
  • @Andrew Wrong: http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php and I'm sure my problem is exactly this difference – Fane Dec 05 '15 at 16:41
  • I know the difference, but it is who we determine the single or double quote – Andrew Dec 05 '15 at 16:43
  • @Andrew ... so? If it is *we who* determine the single or double quote how exactly can I guarantee my string has a double quote? Read the question carefully – Fane Dec 05 '15 at 16:48
  • i don't know what you're trying to do but why not use `$str = str_replace('\'', '"', $str)` to ensure that all single quotes are replaced by a double? – luke_mclachlan Dec 05 '15 at 22:18

1 Answers1

5

It's just the matter of how you declare your strings in the code.

All these functions (well, actually all functions that return a string) return just a string. Single quote/double quote matters when you specify the string right in the code.

echo '\nHey!'; // outputs `\nHey!`
echo "\nHey!"; //Outputs a blank line and `Hey!`

So, double/single quote matters only when you specify those strings in the code.

trim('\nHey!') // returns `\nHey!`
trim("\nHey!") // returns just `Hey!`

So, if you have some string in, let's say, $_POST['str'], it would be the exact same thing the user typed in the text field. If he put a newline there, it would be a newline, if he typed \n, it would be \n.

You can read more about single/double quote difference in this question, but the only thing that you should keep in mind:

That only matters for strings YOU declare RIGHT IN THE CODE

PHP might sometimes a seem a bit weird language, but it's absolutely sane (except for needle-haystack order :))

Good luck with PHP,

Best regards, Alexander.

Community
  • 1
  • 1
Alexander Mikhalchenko
  • 4,525
  • 3
  • 32
  • 56