SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2005
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to replace slashes?

    Hi, I am trying to use str_replace to replace a forward slash with a back slash. Here is what I am currently using, but I get errors.

    PHP Code:
        $test str_replace("\", "/", $test); 
    I've also tried this

    PHP Code:

        $test 
    str_replace("\"""/"", $test); 
    Any help would be appreciated, thanks.

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    $test str_replace('\\','/'$test); 

  3. #3
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gannina
    Hi, I am trying to use str_replace to replace a forward slash with a back slash.
    PHP Code:
    $test str_replace('/''\\'$test);
    // or
    $test str_replace('/'"\\"$test); 
    Christopher

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The backslash is an escape character. (\" means " and is useful if you need to enter double quotes inside a quoted string.)

    That means it has to be escaped when you want to specify a literal backslash, so \\ means \ inside a string.

    Either of arborint's suggestions will work. (You may want to look up the argument order of str_replace.)
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2005
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thx for the help. I kinda got confused with the which one to escape

  6. #6
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    I've found a bunch of these sites and they all seem to show the same methods. Thing is, if you use them and the "/" is in front of a word, the first letter of that word gets chopped off with whatever you try to replace it with, whether it's slashes or even text. How do you solve that problem?

    I've been using w3schools.com/jsref/tryit.asp?filename=tryjsref_replace_regexp to try and figure this one out. Anyone willing to try it out to tell me how to keep the first letter from getting chopped off?

    Thanks,
    -Tom

  7. #7
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <html>
    <head>
    <title>Javascript String Replace Path</title>
    <script type="text/javascript">
    <!--
    function regexpDemo(monkey,testNum) {
    switch (testNum) {

    case 5:
    monkey.value = monkey.value.replace(/file:\/\/\/C\|/gi,"C:").replace(new RegExp(/\//gi),"\\");
    break;

    case 6:
    monkey.value = monkey.value.replace(/C:/gi,"file:///C|").replace(new RegExp(/\\/gi),"/");
    break;
    default:
    break;
    }
    }
    // -->
    </script>
    </head>
    <body>

    <form id="demo5" action="#" onsubmit="regexpDemo(this.monkey,5);return false">
    <div><input name="monkey" type="text" size="50" value="file:///C|/WINDOWS/system32/cmd.exe" />
    <input type="submit" value="C:\" class="buttonHi" style="width:60px;" />
    <input type="reset" value="Reset" class="buttonHi" style="width:60px;" /></div>
    </form>

    <form id="demo6" action="#" onsubmit="regexpDemo(this.monkey,6);return false">
    <div><input name="monkey" type="text" size="50" value="C:\WINDOWS\system32\cmd.exe" />
    <input type="submit" value="file:///C|" class="buttonHi" style="width:60px;" />
    <input type="reset" value="Reset" class="buttonHi" style="width:60px;" /></div>
    </form>

    </body>
    </html>

  8. #8
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks, blueprintsxp. Great example on that, and works great on that file, but for some reason when I try to integrate it into my own, it doesn't like it:

    <html>
    <body>
    <script type="text/javascript">

    var str = "The Lord of \the Rings: The Fellowship of the Ring";

    var result = str.replace(new RegExp(/\//gi),"\\");

    document.write(result);

    </script>
    </body>
    </html>

    (using w3schools.com/jsref/tryit.asp?filename=tryjsref_replace_regexp, it gives "The Lord of he Rings: The Fellowship of the Ring" - the "t" is missing where I put the slash in the original string)

    I also noticed that ".replace" seems to not always grab all of the instances it is referenced and replace them.

    So, instead, I found this Substring Replace function from this site:

    breakingpar.com/bkp/home.nsf/0/45A5696524D222C387256AFB0013D850

    It is awesome and will replace anything, anywhere, for however many times it finds it. In case they ever take it off their site, here it is :

    replaceSubstring("hellothere", "l", "x") = "hexxothere"
    replaceSubstring("this is a string", "is", "x") = "thx x a string"
    replaceSubstring("Here is a long string", "", "xxx") = "Here is a long string"
    replaceSubstring("Here is a long string", " ", "") = "Hereisalongstring"
    ' The slash is a literal character, so "\\" is a single slash
    replaceSubstring("\\mysubdir\\mydatabase.nsf", "\\", "/") = "/mysubdir/mydatabase.nsf"
    ' If the string is "literal \s\w characters", it must be represented literally like "literal \\s\\w characters"
    replaceSubstring("literal \\s\\w characters", "\\", "\\\\") = "literal \\s\\w characters"
    replaceSubstring("Getting rid of unwanted words", "unwanted", "unneeded") = "Getting rid of unneeded words"

    Here is the function:

    Code:
    function replaceSubstring(inputString, fromString, toString) {
       // Goes through the inputString and replaces every occurrence of fromString with toString
       var temp = inputString;
       if (fromString == "") {
          return inputString;
       }
       if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
          while (temp.indexOf(fromString) != -1) {
             var toTheLeft = temp.substring(0, temp.indexOf(fromString));
             var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
             temp = toTheLeft + toString + toTheRight;
          }
       } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
          var midStrings = new Array("~", "`", "_", "^", "#");
          var midStringLen = 1;
          var midString = "";
          // Find a string that doesn't exist in the inputString to be used
          // as an "inbetween" string
          while (midString == "") {
             for (var i=0; i < midStrings.length; i++) {
                var tempMidString = "";
                for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
                if (fromString.indexOf(tempMidString) == -1) {
                   midString = tempMidString;
                   i = midStrings.length + 1;
                }
             }
          } // Keep on going until we build an "inbetween" string that doesn't exist
          // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
          while (temp.indexOf(fromString) != -1) {
             var toTheLeft = temp.substring(0, temp.indexOf(fromString));
             var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
             temp = toTheLeft + midString + toTheRight;
          }
          // Next, replace the "inbetween" string with the "toString"
          while (temp.indexOf(midString) != -1) {
             var toTheLeft = temp.substring(0, temp.indexOf(midString));
             var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
             temp = toTheLeft + toString + toTheRight;
          }
       } // Ends the check to see if the string being replaced is part of the replacement string or not
       return temp; // Send the updated string back to the user
    } // Ends the "replaceSubstring" function

  9. #9
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    navyjax2,

    1. i do not understand clearly why exactly the:
    .replace(new RegExp(/\\/gi),"/");
    and the:
    replace(new RegExp(/\//gi),"\\");
    worked to make forward slash and backward slash when couched into the script.

    2. I too have been looking at the example you cite at w3schools.com/jsref/jsref_replace.asp w3schools and i also was not able to make the backslash work in that string. I suspect something of a hurdle put in place by the original writer(s) of javascript which allows the backslash in some specific strings and disallows the backslash in other strings.

    3. I will take a look at length at the page you cited at breakingpar.com and see if i can figure out ANYTHING there. At first glance it is fascinating. This is as time allows.

    4. First I am going to try to figure out the project i am on today with Ben of:
    bennadel.com/index.cfm?dax=blog:142.view
    Now I am trying to make an 8.3 path, a short path, using the Javascript String Replace Method to produce as a result (example):

    C:\PROGRA~1\INTERN~1\iexplore.exe
    from:
    C:\Program Files\Internet Explorer\iexplore.exe

    navyjax2 i will look into your question, eventually,

    Regards,
    blueprintsxp

  10. #10
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No rush at all.... The function I posted above works wonders. I'm content with my magic box . At least I know now what should work - apparently the people that invented JavaScript didn't decide to stay consistent somehow? I think it's a bug in JavaScript's language, personally... lol... Take it easy. Thanks for efforts.

    -Tom


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •