Search code examples
phpreplacebackslash

Remove backslashes from a string


I'm trying to replace \/ with / a string in PHP.

The string I have is:

http:\/\/xxx.xxx\/xxx

I want to replace the

\/

with just

/

(to give http://xxx.xxx/xxx).

All the escapes and stuff are confusing me!

I've tried every combination I can think of. I thought:

$str = preg_replace("\ \\ \/ /", "/ \/ /", $str);

would do the trick (removing the spaces) but no luck.


Solution

  • Regex add complexity, use a simple str_replace:

    $str = str_replace('\/', '/', $str);
    

    Test

    $str="http:\/\/xxx.xxx\/xxx";
    $str= str_replace("\/", "/", $str);
    echo $str;
    

    Output

    http://xxx.xxx/xxx