Iain Simpson
Iain Simpson

Reputation: 469

Using html with style tags in php code

I have been given the following as a template for use in an email. I am using phpmailer to mail it out, but am having a problem with the way style tags begin and end as they are conflicting with the php. If I go through thier template, I could move all the styles into a seporate style sheet or use classes and put the styles at the top of the page, but I dont want to do this unless I really have to.

Below is a generic example of what is going wrong with the code. The font names are wrapped in "s so the opening and closing tags of the style are 's , these 's are clashing with the opening and closing tags of php. I cant seem to find a way round it though as if I put, style=""font-name","another-font"" this wont work, if I use 's at all php is then screwed up.

What is happening is in php mailer the form contents are declared as

$body = '<span style='font-size: 13.5pt;font-family:"Georgia","serif";color:white'>some content</span></html>';

Upvotes: 0

Views: 15232

Answers (3)

Rudi Visser
Rudi Visser

Reputation: 21979

Don't do this.

Your issue is not using HTML/CSS in PHP, it's just that your strings are basically un-stringing themselves, you need to escape the quotes as @ddubs said.

However, the elaborate on the "Don't do this", you shouldn't be mixing your HTML and PHP code like this. Although it's of course not required you should be separating out your logic from presentation as much as possible.

You can simply jump out of PHP with the closing tag, and jump back in when you need it, for example (based on yours):

// code code code ...
?>
<span style="font-size: 13.5pt; font-family: 'Georgia', serif; color: white">
    <?php echo $variable_from_code ?>
</span></html>

Whilst I'm on the whole crusade, you also shouldn't really be using inline styles ;-)

Upvotes: 0

Dirk McQuickly
Dirk McQuickly

Reputation: 2139

You can also work with HEREDOC (http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc). This will save you a lot of messy code. Example:

$string = <<<SOMELABEL
Here you can put your <span style='font-family: arial, helvetica, sans-serif'>formatted code</span>
SOMELABEL;

And then:

$body = $string;

Upvotes: 0

Derek Wright
Derek Wright

Reputation: 1492

You need to escape your quotes. Example:

$body = '<div style=\'background:#000;\'><p>srs business here</p></div>';

Upvotes: 4

Related Questions