How to replace “\” using str_replace() in PHP?

后端 未结 5 1111
暗喜
暗喜 2020-12-11 15:23

I would like to remove all back slashes from strings on my site. I do not wish to use strip_slashes(), because I want to keep forward slashes.

This is the code I am

相关标签:
5条回答
  • 2020-12-11 15:53

    The backslash is actually escaping the closing quote in your string.

    Try echo str_replace("\\","","it\'s Tuesday!");

    0 讨论(0)
  • 2020-12-11 16:02

    With:

    echo str_replace("\'", "'", "it\'s Tuesday!");
    // It's Tuesday!
    
    0 讨论(0)
  • 2020-12-11 16:13

    Try and get the result:

    $str = "it\'s Tuesday!";
    
    $remove_slash = stripslashes($str);
    
    print_r($remove_slash);
    

    Output: it's Tuesday!

    0 讨论(0)
  • 2020-12-11 16:14

    No sure why you are using str_replace to remove \ use

    echo stripslashes("it\'s Tuesday!");
    

    But if its just an example then

    echo  str_replace("\\","","it\'s Tuesday!");
    

    Please Note that stripslashes only remove backslashes not forward

    echo stripslashes("it\'s \\ \\  // Tuesday!");
    

    Outputs

    it's // Tuesday!
    
    0 讨论(0)
  • 2020-12-11 16:14

    From the stripslashes() documentation:

    Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).

    So you shouldn't worry about the fwd. slashes.

    0 讨论(0)
提交回复
热议问题