sophie
sophie

Reputation: 1531

How to replace php replace \ by \\ within PHP?

anybody know how can I replace \ by \\ ?

input string : Télephone dsadad;'''´ ''''aa\

 php> $in =  "Télephone dsadad;'''´ ''''aa\";
 ... echo str_replace("\\","\\\\",$in);
Multiline input has no syntactic completion:
PHP Parse error:  syntax error, unexpected T_NS_SEPARATOR in Command line code on line 2

thanks

Upvotes: 1

Views: 2798

Answers (3)

Mike Dinescu
Mike Dinescu

Reputation: 55720

If you just want to do string replaces:

preg_replace("\\", "\\\\", $string);

Notice that the \ is a special character and has to be escaped!

However, if you're interested in escaping strings, PHP has some built-in functions to do that:

See the addslashes function:

addslashes($string);

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

Um... Same as any other string?

$out = str_replace("\\","\\\\",$in);

The only difference is that you have to escape each \ in the string.

Upvotes: 3

anubhava
anubhava

Reputation: 785146

Use str_replace:

str_replace('/', '//', 'abc/def/xyz');

OUTPUT:

abc//def//xyz

Upvotes: 2

Related Questions