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 “
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.