Guy Fawkes
Guy Fawkes

Reputation: 2441

stripslashes issue when character after two backslashes

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.

Upvotes: 0

Views: 643

Answers (3)

Linus Kleen
Linus Kleen

Reputation: 34612

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.

Upvotes: 2

rauschen
rauschen

Reputation: 3996

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

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

$q = stripslashes($q);

become

This issame \ text

Upvotes: 0

Erik
Erik

Reputation: 4105

Actually

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

contains one backslash.

Upvotes: 0

Related Questions