Replacing Backslashes with Forward Slashes with Str_Replace() in PHP

Replacing backslashes with forward slashes with str_replace() in php

you have to place double-backslash

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

Issue replacing forward slash in string (php)

I figured out a solution! Not that it explains some of the weird str_replace activity that I was seeing, so if anyone can explain that please do.

But to get around the original issue I was experiencing - The line

$filepath = str_replace($site_url.'/', '', $attachment->image_src_large[0]);

was only having an effect on either back or front end but not both because $attachment->image_src_large[0] was different for each. On front end I would get https://... but on back end, for whatever reason, I would get http://....

So I expanded $site_url = get_site_url(); to be:

$site_url = get_site_url();
if (strpos($site_url, 'https') !== false){
$ssl_site_url = $site_url;
$plain_site_url = str_replace("https", "http", $site_url);
} else {
$plain_site_url = $site_url;
$ssl_site_url = str_replace("http", "https", $site_url);
}

and then did a str_replace() for both strings. Might be overkill but it will work no matter what protocol $attachment->image_src_large[0] turns out to be.

PHP str_replace for back slash

Your best chance is to do a mysql replace query:

Change the value in the query:

SELECT REPLACE('http://yourdomain.com', 'http', 'https');

https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace

You might also try this instead:

UPDATE table
SET field_name = replace(field, 'http, 'https')

https://dba.stackexchange.com/questions/2261/how-do-i-remove-backslashes-from-records-in-mysql

Replacing local path backslashes with http:// forward slashes str_replace() in php

Can you please try this,

Simply:

echo $url = str_replace("/\\","/",$url);

another method,

$url = str_replace("C:\inetpub\Music","http://www.mydomain.com/Music/","http://www.mydomain.com/Music/2011 Hits/\Balle Lakka.mp3");

$url = str_replace("\\","/",$url);
$url = str_replace("//","/",$url);
echo $url = str_replace("http:/","http://",$url);

How to replace last backward slash with forward slash PHP

Here is a solution using PHP's string functions instead of regex.

Do this:

$url = 'C:\wamp\www\chm-lib\sekhelp_out\HTML\AS_BUILD.htm';
$pos = strrpos($url, '\\');
$url = substr_replace($url, '/', $pos, 1);

echo $url;

To get this:

C:\wamp\www\chm-lib\sekhelp_out\HTML/AS_BUILD.htm

Explanation:

  1. Get the position of the last \ in the input string using strrpos()
  2. Replace that with / using substr_replace()

Note

It is important to pass '\\' instead of '\' to strrpos() as the first \ escapes the second.

Also note that you can shorten the code above to a single line if you prefer, but I thought it would be easier to understand as is. Anyway, here is the code as a one-liner function:

function reverseLastBackslash($url) {
return substr_replace($url, '/', strrpos($url, '\\'), 1);
}

how to remove space and replace forward slash in php

A simple str_replace can do this:

$var = "A/P/ 20014/03 /12/4098 ";
$var = str_replace(array('/', ' '), array('-', ''), $var);
echo $var;

Illustration:

                        search for        replacement
$var = str_replace(array('/', ' '), array('-', ''), $var);
^ ^ ^ ^
|----|-----------| |
|----------------|


Related Topics



Leave a reply



Submit