简体   繁体   中英

PHP and CSS quotes issue

Hy,

I'm having trouble with quotes. I'm working on a html mail newsletter and it's a nightmare. I've read a lot about how to fix this but nothing won't work. I've tried without quotes, with \\ ... Nothing won't work (maybe I was doing something wrong).

So , let's say this is the code:

echo '<td style="
    background-image:url(?http://x/y/image.png?);
                "
      </td>';

? - what quotes to use

If you want quotes It should be like this, but in this case you don't really need them. If you open with a single quote then you have to escape all single quotes inside youre string.

echo '<td style="
    background-image:url(\'http://x/y/image.png\');
                "
      </td>';

and as far as I know css in html mail is very limited, especially positioning. But I could be wrong.

Quotes are optional in CSS for background url - So simply omit them.

Here's a similar question with more information on that: Is quoting the value of url() really necessary?

Additionally you could escape your quotes by putting a \\ in front of them eg url(\\'http://www.domain.com\\')

As for HTML mail question. Best standards say to stick with tables & inline style CSS. I would avoid any kind of relative/absolute positioning. Even though some email clients may display it correctly, not all will. So better to be consistent as possible and avoid certain markup.

Escape the single quotes in your example code, like this:

echo '<td style="
    background-image:url(\'http://x/y/image.png\');
                "
      </td>';

Use \\ before your quotes to escape the end of string:

echo '<td style="
    background-image:url(\'http://x/y/image.png\');
                "
      </td>';

Quotes are optional : Is quoting the value of url() really necessary?

However, you can escape them:

echo "<td style=\"url('example.com')\"";

or

echo '<td style="url(\'example.com\')";

I don't think this is a problem with selecting the correct quotes. As many others have already pointed out, the quotes are optional or alternatively, you can simply escape a quote and it will still be valid.

I believe the problem is that not all mail clients support background-image , in particular Microsoft Outlook 2007+ and even Outlook.com do not support it.

I recommend consulting the following resources on the level of CSS support for e-mail clients.

One workaround you may want to try is using background instead of background-image .

Also, you might want to try out https://www.mailrox.com/ if you are having problems with building HTML e-mails. I've not tried it myself though so cannot comment on how good the product is.

你不需要在css url()引用。

You could try using the following method:

echo <<<HTML
     <td style="background-image:url('image/url/here.jpg');">Table Data</td>
HTML;

Take a look at PHP EOD if your note sure how to use this: PHP.net Strings

Also make sure you have the correct content-type and charset's set in the E-Mail headers (eg $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\\r\\n"; ) as well as the PHP depending on how you are using the newsletter template.

CSS can be limited when creating e-mails as mail clients vary a lot more than browsers and some support more than others. Give it a go and be sure to check in multiple browsers and e-mail clients.

Hope this helps.

Leon.

You should use heredoc syntax.

echo<<<EOT
    <td style="background-image:url(?http://x/y/image.png?);"
      </td>
EOT;

This is very helpful when you need to use variables, and when you need to print quotes.

Make sure nothing else is on the lines with EOT or this won't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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