duankuaizhe8257 2012-08-07 22:11
浏览 87
已采纳

在php中添加双反斜杠到单个

I have searched a lot and tried javascript replace() function and str_replace,addslashes , strip slashes as well but i am not getting the right output.

This is what im doing:

str_replace("\\","\\\\", "C:wamp\www\desi\uploads\artist\bg\9.jpg";

THe output i am getting is:

C:wampwwwÞsiuploads\A rtist\B g .jpg

Then another way i tried:

var clean=  "<?php echo str_replace("\\","@",LINKCONSTANT); ?>".replace("@","\\");

Still not working any idea ?

  • 写回答

2条回答 默认 最新

  • dqyat62284 2012-08-07 22:21
    关注

    the problem isn't solvable by using str_replace like that because the string in double quotes will have the slashes processed by PHP as escape sequences. Using str_replace like this isn't going to solve the problem of PHP (or javascript) handling string escapes - once you have got a backslash into a string it will stay there quite faithfully.

    However string constants will work in single quotes as follows:

    'C:wamp\www\desi\uploads\artist\bg\9.jpg'
    

    but as pointed out elsewhere the directory separator in windows is internally handled as either "/" or "\" so just use the "/" (for api calls) and you'll be fine.

    If you wish to output a string that is safe to be parsed by javascript then do:

    echo "var str = ".json_encode('C:wamp\www\desi\uploads\artist\bg\9.jpg').";";
    

    which will output in a javascript compatible way:

    var str = "C:wamp\\www\\desi\\uploads\\artist\\bg\\9.jpg";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改