如何修复重音字符的显示?

时间:2017-03-29 19:16:00

标签: perl

我意识到这是一个较旧的脚本,但我很感激 帮助它正常工作的任何帮助。多年前它奏效了 很好,但最近它表现得很有趣。 请原谅格式....

当我在浏览器中运行它时,法语重音字符 不正确显示。但是在它生成的文本文件中, 他们表现得很好。这里发生了什么?

Perl版本是v5.20.2 我的数据库是MySQL,utf8_unicode_ci 该表也是utf8_unicode_ci

浏览器中的

Hélène Rollan和文件:Hélène Rollan

浏览器中的

Coeur à l`écoute和文件:Coeur à l`écoute

#!/usr/bin/perl --
use utf8;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw (:standard);
$q = new CGI;
use Encode;
use open ':encoding(utf8)';
binmode(STDOUT, ":unix:utf8");

$user = "manager";

require "config.cgi";
&Create_DB_Connection;
$time = time(); 

sub Create_DB_Connection{
  use DBI;
  $DSN  = "DBI:mysql:$mysql_database:$mysql_hostname";
  $dbh  = DBI->connect($DSN, "$mysql_username", "$mysql_password", {mysql_enable_utf8 => 1}) || die return;
  if ($dbh) { $connected = "YES"; }
  return;
}

sub Do_SQL{
  eval{
    $sth = $dbh->prepare($SQL);
  };
$dbh->do(qq{SET NAMES 'utf8';});
    $sth->execute;
    return ($sth);
}

    &downline;

# Prepare and show tree of affiliates
sub downline {
($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime(time());
$month = $mon;
$year = $year + 1900;
print $q->header;

print<<EOF;
    <HTML><HEAD><TITLE>Network Summary</TITLE>
<meta http-equiv="Content-type" content="text/html;charset=utf-8" />
</HEAD>
<style type="text/css"> 
<!-- 
A:visited {text-decoration: none;} 
A:hover {text-decoration: underline;}
A:link {text-decoration: none;} 
A:active {text-decoration: none;} 
.MEMBERS {font-size: 8pt; text-decoration: none; font-family: Verdana,verdana; color: FF0000;}
--> 
</style>
<BODY BGCOLOR="FFFFFF" TEXT="000000" LINK="000080" VLINK="004080">
<center><font size=4 face=arial color=000000><b>Network Summary as of $month/$year</b><br>
<font face=verdana,arial size=1><a href=wmstats_en2.cgi>[return to main page]</a></font></center>
<p>
EOF

$featured_file = "/home/bruce/data/featured.txt";

$SQL="SELECT FIRST_NAME,LAST_NAME,SPONSOR_ID,CO_SPONSOR_ID,ID FROM main_members"; 
&Do_SQL;
  while ($row = $sth->fetchrow_hashref){
  $info{$row->{'ID'}} = [$row->{'FIRST_NAME'},$row->{'LAST_NAME'},$row->{'SPONSOR_ID'}];
  push @{ $kids{$row->{'CO_SPONSOR_ID'}} }, $row->{'ID'};
}
$kid = "$user";

if (!$kids{$kid}) { 
print<<EOF;
<center><b><font size=2 face=arial>There are currently no members in your downline</font></b><font size=4 face=arial color=000000><br><BR>
EOF
} else {
&crunch(1);
}

$o++;

sub crunch {

foreach $kid (@{ $kids{$kid} }) {
$newlevel++;
$payouts{$newlevel}++;
$levels{$newlevel}++; 
$total_downline++;
while ($b < $newlevel) { $report .= "&nbsp;&nbsp;&nbsp;&nbsp;"; $b++; } $b=0; 
$report .= "$newlevel: $kid - $info{$kid}[0] $info{$kid}[1] <br>";

# I added this to generate a text file
open (FILE, ">>$featured_file");
flock(FILE, 2);
print FILE "$newlevel: $kid - $info{$kid}[0] $info{$kid}[1] \n";
flock(FILE, 8);
close (FILE);

&crunch($newlevel);
$newlevel--;
delete($info{$kid});
} 
}
print<<EOF;
<center><table><tr><td valign=top nowrap>
<font face=verdana size=2>
$report
</td><td valign=top>
<table cellpadding=0><tr>
<td align=right nowrap><font face=verdana,arial size=2><b>Total Downline:<p>&nbsp;</td><td><font face=verdana,arial size=2><b>&nbsp; $total_downline<p>&nbsp;</td></tr>
EOF

while (!$found_some) { $i++;
if ($levels{$i}) {
print<<EOF;
<tr><td align=right><font face=verdana,arial size=2><b>Level $i:</td><td><font face=verdana,arial size=2><b>&nbsp; $levels{$i}</td></tr>
EOF
} else { $found_some = 1; }
}

print<<EOF;
</td></tr></table>
<p><font face=verdana size=2><b>
</TD></TR></TABLE></TD></TR></TABLE>
EOF
}

1 个答案:

答案 0 :(得分:1)

删除不相关的位并修复问题后,您将获得以下内容。 (如果您从问题本身中删除了不相关的位,则会更容易看到差异。)

# Specifies the file is encoded using UTF-8.
# This doesn't matter for this program.
use utf8;

# Set the encoding for STDIN, STDOUT and STDERR.
# Set the default encoding for file handles.
use open ':std', ':encoding(utf-8)';

use CGI      qw( );
use DBI      qw( );
use FindBin  qw( $RealBin );
use Template qw( );

my $cgi = CGI->new();

my $dbh = DBI->connect(
   "dbi:mysql:...",
   "...",
   "...",
   {
       RaiseError => 1,
       PrintError => 0,
       PrintWarn  => 1,

       # Decodes strings from the database.
       # Specifying this now performs SET NAMES 'UTF8'
       mysql_enable_utf8 => 1,
   },
);

my $val = $dbh->selectrow_array('SELECT ...');

{
   open(my $fh, '>', '...')
      or die(...);

   print($fh $val);
}

{
    print $cgi->header('text/html; charset=UTF-8');

    my %vars = (
       val => $val,
    );

    my $tt = Template->new({
       ENCODING     => "UTF-8",
       INCLUDE_PATH => "$RealBin/tmpl",
    });

    $tt->process('test.tmpl', \%vars)
       or die($tt->error());
}

tmpl/test.tmpl

<html>
<head>
<title>Test</title>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
</head>
<body>
[% val | html %]
</body>
</html>

当然,假设数据库中的数据是正确的。我要求验证的请求没有得到答复。