0

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.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
Guy Fawkes
  • 2,313
  • 2
  • 22
  • 39

3 Answers3

2

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.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
  • Oh, I understand it... Thanks. But if I have input string like this: $q = 'This is\same text', how can I to addslashes (4 times) to stripslashes next time? – Guy Fawkes Jan 23 '12 at 16:14
  • Sorry, your example is invalid, you add space after "is" in it. Try to write $q='This is\\some text' and you get same effect as in double quotes. – Guy Fawkes Jan 23 '12 at 16:16
  • The spaces are irrelevant. The *relevant* part is that two backslashes in a double-quoted string evaluate to *one* backslash, whereas one backslash in a single-quoted string *remains* one backslash. – Linus Kleen Jan 23 '12 at 16:20
0

Actually

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

contains one backslash.

Erik
  • 4,120
  • 2
  • 27
  • 20
0

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

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

$q = stripslashes($q);

become

This issame \ text
rauschen
  • 3,956
  • 2
  • 13
  • 13