Htmlspecialchars converting an apostrophe to & # 039; for facebook tag og: title

I have a little fight with the og: title tag on my site. Whenever the title of an article has an apostrophe and is shared on Facebook, ' appears instead of the apostrophe. I compared my meta tags with another site that successfully displays its apostrophes, and the only difference I can find is this:

The code for the apostrophes, placed in the og: title for my site: '

The code for the apostrophes, placed in the og: title for the friend’s site: '

I think my site converts an ampersand after it has already converted an apostrophe, and this makes it display as ' on Facebook.

I use K2 for Joomla !, and asked this question in my forums, but I did not receive a single answer after 5 days.

This is the code I found in the file com_k2> views> item> view.html.php:

 $document->setMetaData('og:title', htmlspecialchars($document->getTitle(), ENT_QUOTES, 'UTF-8')); 

I do not fit php, can I change this so that the code appears correctly? If this helps, here is a link to the article with an apostrophe in the header from the site.

Thank you for your time.

+4
source share
1 answer

See the documentation for htmlspecialchars and see a description of when it converts a single quote (often used as an apostrophe). It will encode only one quote if ENT_QUOTES set and you set ENT_QUOTES in your htmlspecialchars call.

If you do not want to encode a single quote, you just need to stop using this flag and switch it to ENT_COMPAT (which encodes a double quote, but not a single quote), so your statement becomes

 $document->setMetaData('og:title', htmlspecialchars($document->getTitle(), ENT_COMPAT, 'UTF-8')); 

(I added line breaks to make them 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 . By default, it is true , but if you set this parameter to false , then PHP will not encode existing entities, therefore, even if you encoded a single quote, the resulting ' would not be further encoded to ' by further calls to htmlspecialchars, it would remain as ' .

+3
source

Source: https://habr.com/ru/post/1479399/


All Articles