phpapiamazon-product-api

php php_replace regex \\\ forward slash


I'm calling an Amazon API to get a URL to book cover thumbnails.

The thumbnail URL comes back like this:

\\ebc.amazon.com\images\EtcLkasff-_-23.jpg

The forward slashes are the problem. To make these images show up on my page is replace the "\" with a "/". But, my regex php_replace syntax is failing me.

I've got this:

$thumbnail = str_replace('\', '//', $apiString);

I've tried

$thumbnail = str_replace('\\', '//', $apiString);

and it still doesn't work.

Thanks in advance, everyone!!

EDIT:::

after I do:

$thumbnail = "http://" . $thumbnail;

The URL looks like this:

http://\/\/ecx.images-amazon.com\/images\/I\/51vCLCTcmAL._SL75_.jpg

I've tried

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

This gives me an escape error.

I'm sorry :( One more edit to make things perfectly clear:

I've done this:

       // get amazon picture URL
        $picture1 = preg_split('/,/', str_replace("\"", "", $details));
        $picture2 = preg_split('/:/', $picture1[7]);
        $picture3 = preg_replace('/\\//','/',$picture2[2]);
        $picture4 = preg_replace('/\\//','/',$picture3);
//        $picture4 = str_replace('\', '/', $picture4);
        $picture4 = "http://" . $picture4;

         echo "<pre>";print_r($picture4);echo "</pre>";

At this point, $picture4 is:

http://\/\/ecx.images-amazon.com\/images\/I\/51vCLCTcmAL.SL75.jpg


Solution

  • This should really be all you need:

    $thumbnail = str_replace('\\', '/', $apiString);
    //                        ^^^ the backslash must be escaped
    

    This would convert:

    \\ebc.amazon.com\images\EtcLkasff-_-23.jpg

    to

    //ebc.amazon.com/images/EtcLkasff-_-23.jpg