tags:

views:

110

answers:

3

I'm now doing it this way:

<a title="<?php echo $title; ?>">...

But it will brreak when " is included in $title.

+3  A: 

You should run that through htmlspecialchars first to make sure your HTML won't break.

Josh Leitzel
+6  A: 

Not that it's "the final solution", but obviously you need to escape any literal string that isn't mean to contain HTML. In this case:

<a title="<?php echo htmlspecialchars($title); ?>">
Lukáš Lalinský
Actually you can have HTML in the title attribute. Just make sure you replace all quotes with "... I've used this jQuery tooltip (http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery) and I have included all types of HTML markup.
fudgey
Yes, well, you can in HTML and you can't in XHTML. Even in HTML, you need to escape the ampersand so that it's not evaluated as an entity. In general it's a better idea to escape everything, even if you are using HTML. It shouldn't affect the tooltip plugin, because it's looking at the parsed value, not the raw HTML file.
Lukáš Lalinský
+2  A: 

You should translate special characters into HTML entities first, easily done with htmlentities().

<a title="<?php echo htmlentities($title); ?>">
Mikael Auno
Don't use htmlentities() if you don't have to. Use htmlspecialchars() instead. htmlentities() will encode some chars even if we don't need to, thus wasting space.
e-t172