protected function detectCharset()
{
// set the default
$encode = new Encode();
$encode->from($this->defaultCharset);
$encode->to($this->defaultCharset);
if (!is_null($this->options->enforceEncoding)) {
// they want to enforce the given encoding
$encode->from($this->options->enforceEncoding);
$encode->to($this->options->enforceEncoding);
return false;
}
$meta = $this->root->find('meta[http-equiv=Content-Type]', 0);
if (is_null($meta)) {
// could not find meta tag
$this->root->propagateEncoding($encode);
return false;
}
$content = $meta->content;
if (empty($content)) {
// could not find content
$this->root->propagateEncoding($encode);
return false;
}
$matches = [];
if (preg_match('/charset=(.+)/', $content, $matches)) {
$encode->from(trim($matches[1]));
$this->root->propagateEncoding($encode);
return true;
}
// no charset found
$this->root->propagateEncoding($encode);
return false;
}