0

Possible Duplicate:
php preg_replace \

I am trying to replace all occurances of the "\" character with "_c" using preg_replace.

Here is some code I have tried:

$outputStr=preg_replace('/\/','_c',$inputStr);
$outputStr=preg_replace('/\\/','_c',$inputStr);

But the $outputStr ends up as NULL in both cases. What is the correct regular expression to get the "\" character?

1
  • i searched stackoverflow for this problem - i must of missed this one somehow! sorry! Commented Apr 2, 2012 at 10:48

2 Answers 2

2

You need to escape the slash in your string and your regex:

To use backslash in replacement, it must be doubled ("\\\\" PHP string).

See http://de3.php.net/preg_replace. e.g.

$outputStr = preg_replace('/\\\\/','_c',$inputStr);
Sign up to request clarification or add additional context in comments.

Comments

0

you just miss one '\' :

$outputStr=preg_replace('/\\\/','_c',$inputStr);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.