PHP resolves html class library simple_html_Transcoding bug from dom
- 2021-06-28 11:54:09
- OfStack
simple_is in use these dayshtml_dom grabs a few articles.The codes of different websites are essentially gbk gb2312 utf-8 in China.Most of them were gb2312 and utf-8.
My version of simple_html_dom has a method convert_This is how text looks.
// PaperG - Function to convert the text from one character set to another if the two sets are not the same.
function convert_text($text)
{
global $debug_object;
if (is_object($debug_object)) {$debug_object->debug_log_entry(1);}
$converted_text = $text;
$sourceCharset = "";
$targetCharset = "";
if ($this->dom)
{
$sourceCharset = strtoupper($this->dom->_charset);
$targetCharset = strtoupper($this->dom->_target_charset);
}
if (is_object($debug_object)) {$debug_object->debug_log(3, "source charset: " . $sourceCharset . " target charaset: " . $targetCharset);}
if (!empty($sourceCharset) && !empty($targetCharset) && (strcasecmp($sourceCharset, $targetCharset) != 0))
{
// Check if the reported encoding could have been incorrect and the text is actually already UTF-8
if ((strcasecmp($targetCharset, 'UTF-8') == 0) && ($this->is_utf8($text)))
{
$converted_text = $text;
}
else
{
$converted_text = iconv($sourceCharset, $targetCharset, $text);
}
}
// Lets make sure that we don't have that silly BOM issue with any of the utf-8 text we output.
if ($targetCharset == 'UTF-8')
{
if (substr($converted_text, 0, 3) == "\xef\xbb\xbf")
{
$converted_text = substr($converted_text, 3);
}
if (substr($converted_text, -3) == "\xef\xbb\xbf")
{
$converted_text = substr($converted_text, 0, -3);
}
}
return $converted_text;
}
Look at this line:
$converted_text = iconv($sourceCharset, $targetCharset, $text);
This will cause incorrect transcoding.For example, the text of gb2312 would be converted to:
4 month 26 Day in <span style="color:#C03"> Collateral </span> At the Park Equestrian Range 2014 In the qualifying match of the Chinese Langqin International Horse Federation Stadium Barrier World Cup, 24 Han Zhuangzhuang, aged 18, not only scored a zero penalty ... No. 7 Of them <span style="color:#C03"> Contains </span> Olympic Rider Zhao Zhiwen Di 1 Zero penalties per harvest, time consuming 77 second 07 ...
It is a fact that the transcoding function inside is not handled properly.Because I use this simple_html_dom just wanted to build dom.I don't intend to spend time doing a good job with this bug.But simply
$converted_text = iconv($sourceCharset, $targetCharset, $text);
Change to
$converted_text = $text;
That's it.The idea is to cancel its transcoding.Okay, work doesn't have to be tangled, it can go on.