[英]How to save a ® (registered trademark) in Perl ASP SQLite database
I can not isolate this problem. 我无法隔离这个问题。 I have an HTML form text input that is being saved into an SQLite datebase via Perl ASP.
我有一个HTML表单文本输入,该输入通过Perl ASP保存到SQLite的日期数据库中。 If I just save the form data
®
or if replace the character by using: 如果我仅保存表单数据
®
或使用以下方法替换字符:
$registered = chr(174);
$DESCRIPTION =~ s/$registered/R/g;
I get an extra character when the data is retreived ®
or ÂR
if I replace the trademark with the code above, save it again and I get î
, again ÃÂî
. 我得到的数据时retreived一个多余的字符
®
或ÂR
如果我的代码替换上面的商标,再次保存它,我得到î
,再次ÃÂî
。 Where are the ÃÂ
's coming from?? ÃÂ
来自哪里?
Set the sqlite_unicode attribute to 1 in your connect: 在连接中将sqlite_unicode属性设置为1:
$dbh = DBI::connect( "dbi:SQLite:dbname=foo", "", "", { sqlite_unicode => 1 } );
After that, when setting some binary data columns, you may need to explicitly denote them as binary: 之后,设置一些二进制数据列时,您可能需要将它们明确表示为二进制:
$sth->bind_param(1, $binary_data, SQL_BLOB);
The string is probably in UTF-8 (Perl's standard for character encoding) when you are working with it. 使用该字符串时,它可能采用UTF-8(Perl的字符编码标准)。 A registered trademark symbol in UTF-8 is two bytes, and you are only replacing one of them.
UTF-8中的注册商标符号是两个字节,您只需要替换其中一个即可。 See more information here for the encoding of that character .
请参阅此处有关该字符编码的更多信息 。
If you want to replace the symbol with a regex, use a method other than chr()
to match the appropriate character. 如果要用正则表达式替换符号,请使用
chr()
以外的方法来匹配适当的字符。 You should be able to do this: 您应该可以执行以下操作:
s/\x{c2ae}/R/g;
\\x
matches a UTF-8 character given in hexadecimal. \\x
匹配以十六进制给出的UTF-8字符。 I obtained the hex encoding from the page linked above. 我从上面链接的页面获得了十六进制编码。
For more information see "Escape Sequences" in perlre . 有关更多信息,请参见perlre中的 “转义序列”。
Also see the Encode
core module for more information on how Perl handles character encodings. 另请参阅
Encode
核心模块,以获取有关Perl如何处理字符编码的更多信息。
Maybe this tour will shed some light on what you are hitting? 也许这次巡回演唱会能使您更清楚地了解到您正在击中的东西吗? I'm guessing
chr2
is where your issue lies. 我猜
chr2
是您的问题所在。
use strictures;
use utf8;
use DBI;
my $dbh = DBI->connect("dbi:SQLite::memory:", undef, undef,
{ sqlite_unicode => 1,
PrintError => 1 } );
$dbh->do(<<"");
CREATE TABLE moo (
name TEXT
,string TEXT )
my $insert = $dbh->prepare("INSERT INTO moo VALUES ( ?, ? )");
my %reg = ( raw => "®", # note "use utf8;"
"chr" => chr(174) );
while ( my ( $name, $reg ) = each %reg )
{
$insert->execute($name, $reg);
}
# And a couple without placeholders (which we all know is EVIL, right?)
$dbh->do(<<"");
INSERT INTO moo VALUES( "raw2", "®" )
my $reg = chr(174);
$dbh->do(<<"");
INSERT INTO moo VALUES( "chr2", "$reg" )
my $sth = $dbh->prepare("SELECT * FROM moo");
$sth->execute;
binmode STDOUT, ":encoding(UTF-8)";
while ( my $row = $sth->fetchrow_hashref )
{
print $row->{name}, " -> ", $row->{string}, $/;
}
__DATA__
chr -> ®
raw -> ®
raw2 -> ®
"\x{00ae}" does not map to utf8.
chr2 -> \xAE
After taking a look at the characters actually in the string with: 用以下命令查看字符串中的实际字符后:
foreach (split //, $DESCRIPTION) {
$hold = ord($_);
%>chr(<%= $hold %>)<br><%
}
I found that ®
from the html form text input is being treated/received as chr(194).chr(174). 我发现html表单文本输入中的
®
被视为/接收为chr(194).chr(174)。 So: 所以:
$registered = chr(194).chr(174);
$DESCRIPTION =~ s/$registered/®/g;
allows me to save it to the database without issue... 允许我将其保存到数据库而不会出现问题...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.