-4

I need to find \ in a string.

Example

$replacevalue = "%20";
$area = "test\abc.htm";
$valuetoreplace = "\";
$area = str_replace($valuetoreplace,$replacevalue, $area );

But it seems like the page goes into a loop or somthing if i do the same with / there are no problem please help

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
marius
  • 1
  • 1

6 Answers6

3

You need to double your \s as it is an escape character for strings... So:

% php
<?php
$area = str_replace("\\","",'test\abc.htm');
echo $area."\n";
?>

Yields... testabc.htm

hd1
  • 33,938
  • 5
  • 80
  • 91
3

try this:

$replacevalue           =   "%20";
$area               =   "test\abc.htm";

$valuetoreplace     =   "\\";
$area               =   str_replace($valuetoreplace,$replacevalue, $area );

or another solution is in this post

Community
  • 1
  • 1
Mark
  • 8,046
  • 15
  • 48
  • 78
2

valuetoreplace should be "\\".

ggbranch
  • 559
  • 6
  • 21
1

Well "\" this character is an escape character so compiler refers to it as an escape not as a "\", just add another one so it will escape itself "\" and your code will work

mr. Holiday
  • 1,780
  • 2
  • 19
  • 37
1

Change the line:

$valuetoreplace     =   "\";

to

$valuetoreplace     =   "\\";

Read more about escaping characters.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
0

'\' is a special character add a double \\ to escape it

Sitati
  • 158
  • 5