Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripslashes and newline in variable in PHP

Tags:

string

php

As I understand, stripslashes should not remove slash from newline character "\n". And all works fine, excepts situation, when I have a newline character in a variable.

$string = '\n\"';
echo stripslashes($string); // n"

But I need in the next output: \n".

Thank you in advance.

like image 233
Alex Pliutau Avatar asked Dec 26 '11 14:12

Alex Pliutau


2 Answers

I think you are mixing double and simple quotes

$bad = '\n\"';
$good = "\n\"";

Using single quote there is no escapement(appart \') also in single quote PHP won't replace PHP variables.

On the other hand the double quote permits using escaped sequence such as \n, \t etc...

You can review the documentation and check the differences.

So you meant to write

$string = "\n\"";
echo stripslashes($string); // \n"
like image 73
RageZ Avatar answered Nov 14 '22 02:11

RageZ


Inside single quotes, \n does not translate to a newline. Use double quotes instead:

$string = "\n\"";

For a better understanding, your current code is equivalent to:

$unwanted_string = "\\n\\\""; // Will be printed as:  \n\"
like image 32
Rob W Avatar answered Nov 14 '22 04:11

Rob W