An empty string activates detection from script encoding (Zend multibyte). It seems that ENT_XML1 and ENT_XHTML are identical when decoding. I had an issue where I was storing text files, with entities converted, into a database.
String htmlentitydecode ( string $string , int $flags = ENTCOMPAT ENTHTML401 , string $encoding = iniget('defaultcharset') ) htmlentitydecode is the opposite of in that it converts HTML entities in the string to their corresponding characters. More precisely, this function decodes all the entities (including all numeric entities) that a) are necessarily valid for the chosen document type — i.e., for XML, this function does not decode named entities that might be defined in some DTD — and b) whose character or characters are in the coded character set associated with the chosen encoding and are permitted in the chosen document type.
All other entities are left as is. Parameters string The input string. Flags A bitmask of one or more of the following flags, which specify how to handle quotes and which document type to use. The default is ENTCOMPAT ENTHTML401. Available flags constants Constant Name Description ENTCOMPAT Will convert double-quotes and leave single-quotes alone. ENTQUOTES Will convert both double and single quotes.
ENTNOQUOTES Will leave both double and single quotes unconverted. ENTHTML401 Handle code as HTML 4.01. ENTXML1 Handle code as XML 1. ENTXHTML Handle code as XHTML. ENTHTML5 Handle code as HTML 5. Encoding An optional argument defining the encoding used when converting characters.
If omitted, the default value of the encoding varies depending on the PHP version in use. In PHP 5.6 and later, the configuration option is used as the default value. PHP 5.4 and 5.5 will use UTF-8 as the default. Earlier versions of PHP use ISO-8859-1.
Although this argument is technically optional, you are highly encouraged to specify the correct value for your code if you are using PHP 5.5 or earlier, or if your configuration option may be set incorrectly for the given input. The following character sets are supported: Supported charsets Charset Aliases Description ISO-8859-1 ISO8859-1 Western European, Latin-1. ISO-8859-5 ISO8859-5 Little used cyrillic charset (Latin/Cyrillic). ISO-8859-15 ISO8859-15 Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1 (ISO-8859-1).
UTF-8 ASCII compatible multi-byte 8-bit Unicode. Cp866 ibm866, 866 DOS-specific Cyrillic charset. Cp1251 Windows-1251, win-1251, 1251 Windows-specific Cyrillic charset. Cp1252 Windows-1252, 1252 Windows specific charset for Western European. KOI8-R koi8-ru, koi8r Russian.
BIG5 950 Traditional Chinese, mainly used in Taiwan. GB2312 936 Simplified Chinese, national standard character set. BIG5-HKSCS Big5 with Hong Kong extensions, Traditional Chinese. ShiftJIS SJIS, SJIS-win, cp932, 932 Japanese EUC-JP EUCJP, eucJP-win Japanese MacRoman Charset that was used by Mac OS. ' An empty string activates detection from script encoding (Zend multibyte), and current locale (see and ), in this order. Not recommended. Note: Any other character sets are not recognized.
The default encoding will be used instead and a warning will be emitted. I wanted to use this function today and I found the documentation, especially about the flags, not particularly helpful. Running the code below, for example, failed because the flag I used was the wrong one.
$string = 'Donna's Bakery'; $title = htmlentitydecode($string, ENTHTML401, 'UTF-8'); echo $title; The correct flag to use in this case is ENTQUOTES. My understanding of the flag to use is the one that would correspond to the expected, converted outcome. So, ENTQUOTES for a character that would be a single or double quote when converted. Please help make the documentation a bit clearer. I wrote in a previous comment that htmlentitydecode only handled about 100 characters. That's not quite true; it only handles entities that exist in the output character set (the third argument).
If you want to get ALL HTML entities, make sure you use ENTQUOTES and set the third argument to 'UTF-8'. If you don't want a UTF-8 string, you'll need to convert it afterward with something like utf8decode, iconv, or mbconvertencoding.
If you're producing XML, which doesn't recognise most HTML entities: When producing a UTF-8 document (the default), then htmlspecialchars(htmlentitydecode($string, ENTQUOTES, 'UTF-8'), ENTNOQUOTES, 'UTF-8') (because you only need to escape and & unless you're printing inside the XML tags themselves). Otherwise, either convert all the named entities to numeric ones, or declare the named entities in the document's DTD. The full list of 252 entities can be found in the HTML 4.01 Spec, or you can cut and paste the function from my site. We were having very peculiar behavior regarding foreign characters such as e-acute. However, it was only showing up as a problem when extracting those characters out of our mysql database and when being displayed through a proxy server of ours that handles dns issues. As other users have made a note of, the default character setting wasn't what they were expecting it to be when they left theirs blank. When we changed our defaultcharset to 'UTF-8', our problems and needs for using functions like these were no longer necessary in handling foreign characters such as e-acute.
Good enough for us! I just ran into the: Bug #27626 htmlentitydecode bug - cannot yet handle MBCS in htmlentitydecode! The simple solution if you're still running PHP 4 is to wrap the htmlentitydecode function with the utf8decode function. By default htmlentitydecode returns the ISO-8859-1 character set, and by default utf8decode. 'Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1'. When using this function, it's a good idea to pay attention when it says that leaving the charset parameter empty is 'not recommended'.
I had an issue where I was storing text files, with entities converted, into a database. When I retrieved them later and ran $textfile = htmlentitydecode($textdata); the entities were NOT decoded. Once I was aware of the problem, I changed the decode call to fully specify all of the parameters: $textfile = htmlentitydecode($textdata, ENTCOMPAT ENTHTML5,'utf-8'); This converted the entities as expected.