html_entity_decode not having any effect

60 views Asked by At

I've read other questions here but none of the answers seems to fix this issue

my MySQL query brings back "Dino's"

$var = Dino& # 39; s; (no spaces);

I need to convert that ''' (no spaces) to "'" a single quote/apostrophe

I have tried the following and none seems to work.

$var = html_entity_decode( $var, ENT_COMPAT, 'UTF-8' );
$var = html_entity_decode( $var, ENT_COMPAT, 'ISO-8859-1' );
$var = html_entity_decode( $var );

Only when I add this line will the remainder of my script work.

$var = str_replace( ''', "'", $var );  

Obviously I may need to to function for other html_entities so I need a solution other than that just above.

What am I overlooking, please?

1

There are 1 answers

1
A.Copter On

Use at least ENT_QUOTES or similar instead of ENT_COMPAT.
As your goal is to convert the (single)-quotes from the html_entity back to character-representation. ENT_COMPAT won't convert single-quotes.

onlinephp.io Demonstration

From php.net:

Constant Name Description
ENT_COMPAT Will convert double-quotes and leave single-quotes alone.
ENT_QUOTES Will convert both double and single quotes.
ENT_NOQUOTES Will leave both double and single quotes unconverted.
ENT_SUBSTITUTE Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or � (otherwise) instead of returning an empty string.
ENT_HTML401 Handle code as HTML 4.01.
ENT_XML1 Handle code as XML 1.
ENT_XHTML Handle code as XHTML.
ENT_HTML5 Handle code as HTML 5.