Re: [RFC] IntlCharsetDetector
| From: | Sara Golemon | Date: | Mon, 11 Apr 2016 23:25:25 +0000 |
| Subject: | Re: [RFC] IntlCharsetDetector | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-92208@lists.php.net to get a copy of this message | ||
On Mon, Apr 11, 2016 at 4:09 PM, Stanislav Malyshev <smalyshev@gmail.com> wrote:
> The API looks a bit strange - new IntlCharsetDetector($text) and then
> detect(). Can't we just have detect([$text])?
>
I went with a direct wrapping of the underlying API because it always
feels like we regret adding magic eventually. It's trivial for some
composer installable library to wrap this into something nicer. In
fact, one probably WOULD use a userspace library in order to provide
fallback to mb_detect_encoding() on PHP < 7.1
That said, how do you feel about compromising by adding this function
in addition to the raw API?
function ucsdet_detect_encoding(string $text, string $hint = null,
bool $filter = false) {
$det = new IntlCharsetDetector($text);
if ($hint !== null) {
$det->setDeclaredEncoding($hint);
}
$det->enableInputFiltering($filter);
return $det->detect()['name'];
}
That'll give simplicity for the 80% case (I have a string, I want a
best guess) but still provide the true API for consideration of
confidence and/or other guesses.
-Sara