我正在调用 Amazon API 以获取用于预订封面缩略图的 URL。
缩略图 URL 像这样返回:
\\ebc.amazon.com\images\EtcLkasff-_-23.jpg
正斜杠是问题所在。要使这些图像显示在我的页面上,请将“\”替换为“/”。但是,我的正则表达式 php_replace 语法让我失望了。
我有这个:
$thumbnail = str_replace('\', '//', $apiString);
我试过了
$thumbnail = str_replace('\\', '//', $apiString);
还是不行。
先谢谢大家!!
编辑:::
在我这样做之后:
$thumbnail = "http://" . $thumbnail;
URL 如下所示:
http://\/\/ecx.images-amazon.com\/images\/I\/51vCLCTcmAL._SL75_.jpg
我试过了
$picture4 = str_replace('\', '/', $picture4);
这给了我一个转义错误。
我很抱歉 :( 再做一次编辑以使事情变得非常清楚:
我这样做过:
// 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>";
此时$picture4是:
http://\/\/ecx.images-amazon.com\/images\/I\/51vCLCTcmAL.SL75.jpg
最佳答案
This应该真的是你所需要的:
$thumbnail = str_replace('\\', '/', $apiString);
// ^^^ the backslash must be escaped
这将转换:
\\ebc.amazon.com\images\EtcLkasff-_-23.jpg
到
//ebc.amazon.com/images/EtcLkasff-_-23.jpg
关于php php_replace 正则表达式\\\正斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6147835/