0

Im having a problem displaying certain data with PHP from the database.

How its currently showing - "SSA's"

How it should show "SSA's"

HTML Meta Tag

meta charset="UTF-8">

PHP Code

$article_title = html_entity_decode(mb_convert_encoding(stripslashes($r->ArticleTitle), "HTML-ENTITIES", 'UTF-8'));
CoderPi
  • 12,985
  • 4
  • 34
  • 62
  • 1
    Is the meta tag incomplete or just a typo? – Raptor Jan 12 '16 at 08:00
  • It sounds like maybe it is double encoded, if you view the source does it show `SSA's` ? you shouldn't need to call stripsplashes on it. – Clay Jan 12 '16 at 08:02
  • If i view the source i get SSA's –  Jan 12 '16 at 08:35
  • So you're needlessly *encoding* to HTML entities somewhere along the line more than once. – deceze Jan 12 '16 at 08:36
  • Ok its fixed the issues when displaying the data in the page. although its only the search bar now that displays the incorrect title. i have a jquery function that takes the title and uses it as a placeholder in the value of the search input. could the jquery.val() function be causing an issue reading the format of the article title ? –  Jan 12 '16 at 08:40
  • 1
    `.val()` displays the value *as is*, while a string with HTML entities *embedded in HTML* causes the entities to be interpreted. The issue is still the same: you have needless HTML entities in your string. See [The Great Escapism (Or: What You Need To Know To Work With Text Within Text)](http://kunststube.net/escapism/). – deceze Jan 12 '16 at 08:46
  • @deceze Thank you for that. i have fixed the issue within the search input. –  Jan 12 '16 at 08:51
  • What fixed the issue? Please accept the answer using the checkbox if it resolves your issue (the checkbox is the thing below the down vote) – Clay Jan 12 '16 at 09:18
  • Thanx @ChrisBanks wasnt aware of that feature –  Jan 12 '16 at 09:36

3 Answers3

1

Remove the html_entity_decode function, as you are double encoding HTML-ENTITIES

And as @ChrisBanks pointed out, you also don't need stripslashes

CoderPi
  • 12,985
  • 4
  • 34
  • 62
  • 1
    If it's double encoded, he actually needs to call `html_entity_decode` again.. kind of strange data issue. – Clay Jan 12 '16 at 08:06
  • May depend on how he is echo'ing `$article_title` - don't have enough information yet to know. – Clay Jan 12 '16 at 08:18
  • @ChrisBanks yes, he would need to give us feedback if that helped – CoderPi Jan 12 '16 at 08:21
  • @ChrisBanks i store the ArticleTitle into the variable $article_title and push it to the html page using Twig. i then call the value using {{article_title | raw}} –  Jan 12 '16 at 08:44
1

You need to call html_entity_decode again because the data is being stored as double encoded and remove the stripslashes.

$article_title = html_entity_decode(html_entity_decode(mb_convert_encoding($r->ArticleTitle, "HTML-ENTITIES", 'UTF-8')));

You might want to investigate how the data is being stored in the database as double-encoded in the first place. Perhaps htmlentities is being called twice somewhere.

To add on to the comment:

You shouldn't store data HTML encoded unless for some reason you really and truly need to (there might be some cases you're required to). It is only on output and rendering on a webpage do you want to use htmlentities.

Clay
  • 4,700
  • 3
  • 33
  • 49
1

You can decode by using these two methods html_entity_decode() or htmlspecialchars_decode()

Basic Example:

$string = html_entity_decode("SSA's");
echo $string; // result SSA's

$string = htmlspecialchars_decode("SSA's");
echo $string; // result SSA's
CoderPi
  • 12,985
  • 4
  • 34
  • 62
devpro
  • 16,184
  • 3
  • 27
  • 38