str_replace "\" with "/"

Posted on 2005-04-18
Last Modified: 2012-06-27
What is the correct way to format and str_replace to change a backslash to a forward slash? I've tried:

str_replace( '\', '/', $string );

But that produces an error.
Question by:MaybeItsJeremy
    LVL 27

    Accepted Solution

    Remember the backslash is the escape character so you have to escape the backslash with another backslash effectively, for example:

    $string = "hello \ there";
    $string = str_replace( '\\', '/', $string );
    echo $string;


    Author Comment

    Hahahahaha. I knew that, but for some reason I kept trying to edit different things on the second parimeter instead of the first. Thank you. :)
    LVL 27

    Expert Comment

    no problem :)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now