John Hoffman
John Hoffman

Reputation: 18617

How do halt repeating back slashes?

My PHP application adds backslashes to quotes in many locations with addslashes(). Unfortunately, this adding has produced output akin to the following.

Every time I refresh my page, this string increases in number of back slashes.

don't
don\'t
don\\'t
don\\\\'t
don\\\\\\\\'t

and so on.

I want to write a function that deletes all these extra back slashes. I have tried

str_replace($text, "\\\\", "");

to no avail.

Upvotes: 0

Views: 196

Answers (3)

Amber
Amber

Reputation: 526613

What happens if your actual value happens to include \\ legitimately? For instance, if your content were referring to a Windows-style share reference - \\192.168.1.1\foo. If you blindly strip out any double slashes, you'll strip out ones that are meant to be there, as well.

The "right" answer is really to not add slashes to things that don't need them. You should know whether a given value is already escaped or not (after all, you're the one controlling where every value comes from, and what it's being used for) and only add slashes when you need them.

Upvotes: 1

zb'
zb'

Reputation: 8059

$text=preg_replace('/\134+/',"\134",$text);

Upvotes: 1

hodl
hodl

Reputation: 1420

You may just use:

 stripslashes($text);

Upvotes: 1

Related Questions