Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing backslashes with forward slashes with str_replace() in php

Tags:

php

I have the following url:

$str = "http://www.domain.com/data/images\flags/en.gif"; 

I'm using str_replace to try and replace backslashes with forward slashes:

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

It doesn't seem to work, this is the result:

http://www.domain.com/data/images\flags/en.gif 
like image 594
Hai Truong IT Avatar asked Jul 24 '11 11:07

Hai Truong IT


People also ask

How do I change the forward slash in regex?

To replace all forward slashes in a string:Call the replace() method, passing it a regular expression that matches all forward slashes as the first parameter and the replacement string as the second. The replace method will return a new string with all forward slashes replaced.

How can I replace multiple characters in a string in PHP?

Approach 1: Using the str_replace() and str_split() functions in PHP. The str_replace() function is used to replace multiple characters in a string and it takes in three parameters. The first parameter is the array of characters to replace.

What is the difference between forward slashes and backslashes?

Summary: The Backslash and Forward SlashThe backslash (\) is mostly used in computing and isn't a punctuation mark. The forward slash (/) can be used in place of “or” in less formal writing. It's also used to write dates, fractions, abbreviations, and URLs.

How do you replace a backslash?

To replace all backslashes in a string:Call the replaceAll() method, passing it a string containing two backslashes as the first parameter and the replacement string as the second. The replaceAll method will return a new string with all backslashes replaced by the provided replacement.


1 Answers

you have to place double-backslash

$str = str_replace('\\', '/', $str); 
like image 172
genesis Avatar answered Sep 21 '22 10:09

genesis