See the documentation for htmlspecialchars and look at the description of when it will convert the single quote (often used as an apostrophe). It will only encode the single-quote if ENT_QUOTES
is set, and you have set ENT_QUOTES
in your invocation of htmlspecialchars.
If you don't want the single-quote to be encoded then you simply need to stop using that flag and switch it for ENT_COMPAT
(which encodes double-quote but not single-quote), so that your statement becomes:
$document->setMetaData('og:title',
htmlspecialchars($document->getTitle(),
ENT_COMPAT, 'UTF-8'));
(I've added the line breaks so it's easy to read on this page, but don't add line breaks in your actual code.)
Also note that htmlspecialchars has a final, optional parameter (added in PHP 5.2.3) called double_encode. It defaults to true
, but if you set this parameter to false
then PHP will not encode existing entities, so even if you did encode the single-quote, the resulting '
would not be further encoded to '
by further invocations of htmlspecialchars, it would be left as '
.