Divining encodings on the Web
nikitathespider.com
nikitathespider.com
For instance, it's frequent to find web sites that contain mostly US-ASCII text with a smattering of ISO-Latin-1 and UTF-8 sequences for characters with codepoints > 127. This happens because, with many toolchains, text can get combined in different encodings.
When I've written this sort of code I've found it most practically effective to look at any explicit document declarations (meta tags), then the BOM, then any XML declarations (XHTML was big once upon a time), then the HTTP headers. Far more people have control over their HTML docs than over their HTTP server configuration and I think it's insane to look at the former and ignore the latter. (Actually, there was one case in which I did so: When the BOM and the HTTP headers agreed, I would ignore meta tags, which are downright wrong more often than you would think. For some reason there are a lot of pages out there encoded in UTF-8 but with meta tags declaring Latin-1 [not vice versa like you might think]. This seemed to be correlated with ColdFusion.)
That said, it's impressive that he's thought through different paths for text/plain and application/xml. The last, most comprehensive function I wrote to do this assumed HTML or XML, which obviously isn't comprehensive, and I missed the text/xml wrinkle.
An XML document of the former type is parsed for the <?xml encoding="..." ?> tag, but the latter isn't?
I always thought that application/xml and text/xml could be used interchangeably. Is my assumption wrong or is his process flawed?
it's a [s]man's[/s] American world :) :(
this is sortof deserved, though :)