Goodbye World
Goodbye World

Reputation: 73

Remove backwards slashes in string

When running a function which returns a string, I end up with backwards-slashes before a quotation marks, like this:

$string = get_string();
// returns: <a href=\"http://example.com/\">Example</a>

I suspect it is some type of escaping happening somewhere. I know I can string replace the backwards-slash, but I suppose in these cases, there is some type of unescape function you run?

Upvotes: 0

Views: 98

Answers (2)

Blue
Blue

Reputation: 22911

You only need to escape quotes when it matches your starting/ending delimiter. This code should work properly:

$string = '<a href="http://example.com/">Example</a>';

If your string is enclosed in single quotes ', then " doesn't need to be escaped. Likewise, the opposite is true.

Avoid using stripslashes(), as it could cause issues if single quotes need to contain slashes. A simple find/replace should work for you:

$string = '<a href=\"http://example.com/\">Example</a>';
$string = str_replace($string, '\"', '"');
echo $string; //echos <a href="http://example.com/">Example</a>

Upvotes: 2

harsh kumar
harsh kumar

Reputation: 165

<?php 

 $string = '<a href=\"http://example.com/\">Example</a>';

 echo stripslashes($string);

?>

Upvotes: 2

Related Questions