Replace backslashes with single bars - Image URL

3

I'm trying to remove the backslashes to open the URL of the image, I found a way to override, but part of my string was lost:

function formatURL( $url )
{    
    echo $url."<br />";

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

    echo $url."<br />";
}

echo "http://10.0.0.1/fotoou/aplic\1893171_1.jpg<br />";

$url = formatURL("http://10.0.0.1/fotoou/aplic\1893171_1.jpg");

echo $url;

The strange thing is that my return is:

http://10.0.0.1/fotoou/aplic93171_1.jpg
http://10.0.0.1/fotoou/aplic93171_1.jpg
http://10.0.0.1/fotoou/aplic/9/8/1893171_1.jpg

Part of the missing string:

\
    
asked by anonymous 27.01.2015 / 17:20

1 answer

3

This is because, in the way you have declared the string, you should have put two backslashes for each bar you want to print. This is called "escaping the bars".

See about this in "Escape Sequences" in the [PHP Manual]. ( link )

In cases of strings declared with double quotation marks (as in your example), using this backslash alone causes PHP to recognize that you are trying to enter a special character.

In this case, you have two options:

  • You can simply replace aspas duplas with aspas simples
  • Or you put two bars instead of two (if you use the double " )
  • Possible changes to your code

    Example 1 (String with double quotation marks):

    $url = formatURL("http://10.0.0.1/fotoou/aplic\1\7\1\3\9\8\1\1893171_1.jpg");
    

    Example 2 (String with single quotation marks):

    $url = formatURL('http://10.0.0.1/fotoou/aplic\1893171_1.jpg');
    
        
    27.01.2015 / 17:24