stripslashes issue when character after two backslashes

647 views Asked by At

I know it's stupid question, but I cannot to google anything for my problem.

I have $q = "This is\\same text"; and do

$q = stripslashes($q);

So, $q is now equal to "This issame text"! How I can to save one backslash?

Thank you.

3

There are 3 answers

3
Linus Kleen On BEST ANSWER

The script does, what it's told, actually.

In $q, the double backslash evaluates to a single backslash (the first escapes the second backslash), which is then stripped away.

If meta-characters are not to be evaluated, you'll need to use single quotes:

$q = 'This is \\some text';
// String is now: This is \\some text

$q = stripslashes($q);
// String is now: This is \some text

EDIT According to your comment in Michaels answer there may be some confusion as to how many valid backslashes there are in your input. Consider the following input:

$q1 = "This is\\\some \text";
$q2 = 'This is\\\some \text';

The first would actually contain This is \\some <TAB>ext. This is due to PHP leaving invalid control characters as-is. \s, as opposed to \t is an invalid control character and is thus left in place.

The second string, however, would literally contain what's in the single quotes, since no evaluation is applied.

0
Erik On

Actually

$q = "This is\\same text";

contains one backslash.

0
rauschen On

If you want that one backslash should stay there, double him

$q = "This is\same \\ text";

$q = stripslashes($q);

become

This issame \ text