Search code examples
phpphp-5.3

addslashes() only if slashes are not added from before


I'm getting a lot of text values from my database that I need to output with slashes added before characters that need to be quoted.

Problem is that some of the data already has the slashes added there from before, whilst some of it doesn't.

How can I add slashes using for example addslashes() - but at the same time make sure that it doesn't add an extra slash in the cases where the slash is already added?

Example:

Input: <a href="test.html">test</a>
Output should be: <a href=\"test.html\">test</a>

Input: <a href=\"test.html\">test</a>
Output should be: <a href=\"test.html\">test</a>

This is PHP 5.3.10.


Solution

  • If you know that you don't have any double slashes, simply run addslashes() and then replace all \\ with \.