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.


	$test = str_replace("\\", "/", $test);

I’ve also tried this



	$test = str_replace("\\"", "/"", $test);

Any help would be appreciated, thanks.

Try this:


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


$test = str_replace('/', '\\\\', $test);
// or
$test = str_replace('/', "\\\\", $test);

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 [fphp]str_replace[/fphp].)

Thx for the help. I kinda got confused with the which one to escape :slight_smile:

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? :eek:

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? :confused:

Thanks,
-Tom

<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>

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 \ he 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 :cool::

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:


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 &lt; midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j &lt; 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

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

No rush at all… The function I posted above works wonders. I’m content with my magic box :cool:. 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