我正在解析一些html,如下所示:
use Modern::Perl;
use Mojo::DOM;
use strict;
use DBI;
use DBD::mysql;
use utf8::all;
my $dbh = DBI->connect("DBI:mysql:db_name", "db_username") or die ("Error:
$DBI::errstr");
my $html = do { local $/; <DATA> };
my $dom = Mojo::DOM->new($html);
my $text = $dom->all_text;
say $text;
my ($var1, $var2, $var3); #normally have values assigned, but not relevant to example
#write $text to MySQL database
$dbh->do("INSERT INTO table_name VALUES (?,?,?,?)",
undef,
$var1, $var2, $var3, $text
);
__DATA__
<div class="field-content"><p> </p>
<p>Here is some data with a trademark html symbol SUPER PRODUCT™, featuring curved
LCD screen.</p></div>将解析后的文本写入标准输出会产生以下结果:
Here is some data with a trademark html symbol SUPER PRODUCT™, featuring curved LCD
screen.一切都很好,这是写给MySQL的。但是,在MySQL中以及从MySQL中提取数据时,我们会得到如下结果:
Here is some data with a trademark html symbol SUPER PRODUCTâ„¢, featuring curved LCD
screen.我怀疑这与Unicode有关,所以尝试更改MySQL状态设置,但仍然没有效果。如何正确地解决这个问题?
发布于 2013-02-05 03:19:10
这句话
#write $text to MySQL database 忽略了许多重要的细节,并且没有包括描述如何从数据库中读取文本的代码,这也很难正确处理。
让我猜测一下,您的数据库中的文本是UTF-8编码的。然后,您需要对该数据库列中的所有内容进行解码:
use Encode;
$decoded_text = Encode::decode("utf-8", $raw_text);
# since you 'use utf8::all', this will get re-encoded to UTF-8 when you write
# it to standard output ...
print "decoded text is $decoded_text\n";https://stackoverflow.com/questions/14693282
复制相似问题