【问题标题】:Single Quotes inside Single Quotes removing Forward Slashes单引号内的单引号删除正斜杠
【发布时间】:2019-06-06 18:49:10
【问题描述】:

我有这个代码:

$profile_image_url = str_replace(" ", "%20", 'https://www.example.com/images/cropped (1).jpg');

$output .= "style='background:url('https://www.example.com/images/" . $profile_image_url . "')no-repeat;'";

输出结果为:

style="background:url(" https: www.example.com images cropped (1).jpg')no-repeat;

如何修复这行代码以防止 URL 斜杠被删除?谢谢!

$output .= "style='background:url('https://www.example.com/images/" . $profile_image_url . "')no-repeat;'";

我试过用反斜杠转义最里面的单引号,但没有用 -->

$output .= "style='background:url(\'https://www.example.com/images/" . $profile_image_url . "\')no-repeat;'";

【问题讨论】:

  • “输出结果” - 中间的代码是什么?你如何回显/vardump $output?无论如何,您在 js 中都会遇到单引号问题:style='background:url('https.. 第二个 ' 需要转义。但这一切都取决于你如何将它输出到哪里。
  • 它在结尾回显“echo $output;”
  • 中间一定有什么,因为这里的单引号style='back 也变成了双引号。请参阅此小提琴:3v4l.org/b9e4C 您在此处显示的代码正在按您的预期工作。
  • 只有问题顶部的 2 行会产生正确的输出。

标签: php escaping double-quotes single-quotes


【解决方案1】:

它并没有真正删除斜线。如果您查看页面源而不是使用检查,则会看到它们。

将样式属性值放在双引号中。

$output .= "style=\"background:url('https://www.example.com/images/" . $profile_image_url . "')no-repeat;\"";

或者将 URL 放在双引号中。

$output .= "style='background:url(\"https://www.example.com/images/" . $profile_image_url . "\")no-repeat;'";

单引号内的未转义单引号是不可能的。遇到的第二个单引号将关闭第一个。如您所见,在这种情况下转义它们是行不通的。

【讨论】:

    猜你喜欢
    • 2022-01-27
    • 2014-06-19
    • 2019-08-11
    • 2014-09-25
    • 2018-11-01
    • 2016-03-24
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多