I have code loading in the titles of some films into the title attribute of their respective thumbnails, this format is thus:
Artist Name - 'Song Title'
I had this code initially:
$return .= "' title='";
$return .=$video['title'];
$return .= "'>";
Obviously the first single quote was causing the title attribute to end prematurley, so I changed it to:
$return .= "' title='";
$return .= htmlspecialchars($video['title']);
$return .= "'>";
Which has had not effect at all, all the titles are still ending prematurely.
Am I misunderstanding the htmlspecialchars method?
http://php.net/manual/en/function.htmlspecialchars.php
By default, htmlspecialchars()
does not escape single quotes (some ancient compatibility behavior). Use htmlspecialchars($video['title'], ENT_QUOTES)
.
You also probably should pass 'utf-8' as the third argument. I wrapped this deprecated behavior like this:
function htmlencode($str, $nl2br = false, $nbsp = false)
{
$str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8', true);
if ($nbsp)
{
$str = str_replace(" ", ' ', $str);
}
if ($nl2br)
{
$str = nl2bruni($str);
}
return $str;
}
/**
PHP's built-in nl2br() only works with LF newlines. This version also works with CR+LF, CR and RS
*/
function nl2bruni($str)
{
return preg_replace('{\?\
|\|\\x1e}', '<br />', $str);
}