简体   繁体   English

如何将图像背景放在变量中?

[英]How to put image background in a variable?

It's kind of a stupid question, but I need to store a div with a background image in a variable. 这是一个愚蠢的问题,但我需要在变量中存储带有背景图像的div My problem arises from the use of " and ' . 我的问题来自于使用"'

This is obviously wrong: 这显然是错误的:

$var = "<div style="background-image:url('bg.png');">";

I have also tried these methods but none worked: 我也尝试过这些方法但没有效果:

$var = "<div style='"."background-image:url('bg.png');".'">";

$var = "<div style='";
$var = $var."background-image:url('bg.png');";
$var = $var."'>";

Does anyone know how I can make it work? 有谁知道我怎么能让它工作?

Thanks. 谢谢。

You can either use single quotes or escape double quotes to avoid any problems 您可以使用单引号或转义双引号来避免任何问题

$var = "<div style=\"background-image:url('bg.png');\">";

Or 要么

$var = '<div style="background-image:url(\'bg.png\');">';

It's the double quotes that's messing with you. 这是双引号,搞砸了你。 You would either have to do \\" or just use single quotes (acceptable in PHP, and more convenient with HTML): 您可能需要执行\\"或仅使用单引号(在PHP中可接受,并且使用HTML更方便):

$var = '<div style="background-image:url(\'bg.png\');">';

With double quotes: 双引号:

$var = "<div style=\"background-image:url('bg.png');\">";

This is valid and will work. 这是有效的,也可以。 Now just use echo $var; 现在只需使用echo $var; to show it. 展示它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM