1

I have an input where users can enter their html which is later displayed as part of the body. To make sure user entered html is properly closed, I am using this code:

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($body);
$dom->removeChild($dom->doctype);
$body = $dom->saveHTML();

$body = str_replace('<html><body>', '', $body);
$body = str_replace('</body></html>', '', $body);
$body = trim(preg_replace('/\s\s+/', ' ', str_replace("\n", " ", $body)));

Now, if the user entered content contains double quotes (“) or single quotes, this is converting it into something like &acirc;&#128;&#156; which is being displayed as “ in the html. How do I make sure special characters like quotes are displayed properly?

EDIT: Here's a sample of text which is being pasted:

It is called a “buddy list,” “friend list” or “contact list." Users are typically alerted when someone on their list is online.

which is being converting into:

It is called a “buddy list,†“friend list†or “contact list." Users are typically alerted when someone on their list is online.
Marius Mucenicu
  • 1,685
  • 2
  • 16
  • 25
Sourabh
  • 1,757
  • 6
  • 21
  • 43
  • 1
    Those look like smart quotes. You could try replacing them - https://stackoverflow.com/questions/1262038/how-to-replace-microsoft-encoded-quotes-in-php – waterloomatt Oct 18 '19 at 18:25

1 Answers1

0

Try

<?php
$str1 = htmlentities($str, ENT_QUOTES);

$str = html_entity_decode($str1);
?>
Kula
  • 16