单引号内的单引号删除正斜杠

Single Quotes inside Single Quotes removing Forward Slashes

我有这个代码:

$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;'";

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

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

$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;'";

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