php文件中的中文乱码该如何解决
程序员文章站
2024-01-19 20:02:40
...
下面的文件的注释部分含有部分中文,但是乱码了。各位大神,知道该如何把乱码部分解析成中文吗?
class Database
{
/*****************************************************
** MySQL????????????
*****************************************************/
var $db;
var $seconds;
var $queries;
var $timer = 0;
var $querytimes = array();
var $duration = '';
public $connectData=true;
/*****************************************************
** ??????,?????????
*****************************************************/
function Database($server = '', $username = '', $password = '', $database = '')
{
global $site_engine_root;
// $this->seconds = $this->currentTime();
if ('' == $server)
{
/*****************************************************
** ????????????????
*****************************************************/
$server = $GLOBALS['db_host'];
$username = $GLOBALS['db_user'];
$password = $GLOBALS['db_pass'];
$database = $GLOBALS['db_name'];
}
/*****************************************************
** ?????????
*****************************************************/
if (!$this->db = @mysql_pconnect($server, $username, $password))
{
if ($username == 'root' && $database == 'siteengine')
{
@header("Location:/install.php");
}
else
{
$this->connectData=false;
//$this->error("database connect fail");
}
}
if ($database&&$this->connectData===true)
{
if($this->version() > '4.1')
{
global $config_charset;
$config_charset = 'utf-8';
if(!$dbcharset && in_array(strtolower($config_charset), array('gbk', 'big5', 'utf-8')))
{
$dbcharset = str_replace('-', '', $config_charset);
}
if($dbcharset)
{
mysql_query("SET NAMES '$dbcharset'");
}
}
if($this->version() > '5.0.1')
{
mysql_query("SET sql_mode=''");
}
$this->selectDb($database);
}
return true;
}
/*****************************************************
** ????????????
*****************************************************/
function disconnect()
{
$result = mysql_close() or $this->error();
return $result;
}
/*****************************************************
** ????????
*****************************************************/
function selectDb($database)
{
global $multidomain,$site_engine_root,$tablepre,$db;
if (@mysql_select_db($database, $this->db))
{
return true;
}
else
{
if ($multidomain==1)
{
$query = $this->query("CREATE DATABASE ".$database);
@header("Location:/install.php");
// require $site_engine_root.'lib/query.php';
// $fp = @fopen($site_engine_root.'data/sql/siteengine.sql', "r");
// $query=@fread($fp, filesize($site_engine_root.'data/sql/siteengine.sql'));
// @fclose($fp);
// if($tablepre!='boka') $query=str_replace("boka_",$tablepre,$query);
// $sql_query = new query($query);
// $sql_query->doquery();
// @header("Location:/index.php");
}
$this->error("$database database connect error!");
}
}
/*****************************************************
** ???????????Web?????????У????????????????С???Web?????·???true???????????·???false
*****************************************************/
function isWebEnv()
{
if (isset($_SERVER['PHP_SELF']))
{
return true;
}
if (isset($GLOBALS['_SERVER']['PHP_SELF']))
{
return true;
}
return false;
}
/*****************************************************
** ???????????????????
*****************************************************/
function _textConvert($text)
{
if ($this->isWebEnv()) // Web????
{
return nl2br($text);
}
return $text;
}
/*****************************************************
** ?????????
*****************************************************/
function currentTime()
{
list($usec, $sec) = explode(' ', microtime());
$time = (double)$sec + (double)$usec;
return $time;
}
function show()
{
$message = "\n" . 'Processed in '
. ($this->currentTime() - $this->seconds) . ' second(s), '
. $this->queries . ' queries.' . "\n";
// return $this->_textConvert($message);
return $message;
}
/*****************************************************
** ?????????????????????
*****************************************************/
function error($var = false)
{
global $domain,$language,$site_engine_root;
// header('Content-Type: text/html; charset="utf-8"');
// @$error = "
";
if ($GLOBALS['sesettings']['system']['errorreport']==1)
{
$errorinfo = mysql_errno().":".rawurlencode(mysql_error());
$time = date("Ymd H:i:s",time());
// @file("http://error.siteengine.net/?domain=$domain&errorinfo=$errorinfo&username=$SESSION[username]&time=$time&language=$language");
}
if (file_exists($site_engine_root.'data/install.lock'))
{
die($this->_textConvert($error));
}
else
{
die($this->_textConvert($error));
// @header("Location:/install.php");
}
// ??????
return $error;
}
/*****************************************************
** ???????sql????
*****************************************************/
function query($sql)
{
global $opendebug,$admincp;
$this->start_timer();
if (($opendebug==1 && $admincp==0) || ($opendebug==2 && $admincp==1))
{
echo ''.$sql.';
';
echo gettotaltime().'
';
// echo $sql.';
';
}
$result = @mysql_query($sql, $this->db) or $this->error($sql);
++$this->queries;// ??????????
$this->querytimes[] = $this->stop_timer();
$lowersql=strtolower($sql);
$lowersql=trim($lowersql);
if(substr($lowersql,0,6) == "update" or substr($lowersql,0,6) == "delete" )
{
//echo "find update";
$affectrows=mysql_affected_rows();
return $affectrows;
}
return $result;
}
/*****************************************************
** ???????sql????
*****************************************************/
function unbufferedQuery($sql)
{
$result = mysql_unbuffered_query($sql, $this->db) or $this->error($sql);
return $result;
}
/*****************************************************
** ????????????????????
*****************************************************/
/*
* $result ??query()????????
* $fetch_mode ????????Ч???
* DB_FETCHMODE_ORDERED (default) ??????????????????
* DB_FETCHMODE_ASSOC ???????????????????
* DB_FETCHMODE_OBJECT ?????????????
*/
function fetchRow($result, $fetch_mode = false)
{
//assert('is_resource($result)');
switch ($fetch_mode)
{
case DB_FETCHMODE_OBJECT:
$row = @mysql_fetch_object($result);//$row->username
break;
case DB_FETCHMODE_ASSOC:
$row = @mysql_fetch_assoc($result);//$row['username']
break;
case DB_FETCHMODE_ORDERED:
default: // ?????
$row = @mysql_fetch_row($result);// 0,1,2,3
break;
}
return $row;
}
}
?>
notepad++ 你试试这个编辑器
class Database
{
/*****************************************************
** MySQL????????????
*****************************************************/
var $db;
var $seconds;
var $queries;
var $timer = 0;
var $querytimes = array();
var $duration = '';
public $connectData=true;
/*****************************************************
** ??????,?????????
*****************************************************/
function Database($server = '', $username = '', $password = '', $database = '')
{
global $site_engine_root;
// $this->seconds = $this->currentTime();
if ('' == $server)
{
/*****************************************************
** ????????????????
*****************************************************/
$server = $GLOBALS['db_host'];
$username = $GLOBALS['db_user'];
$password = $GLOBALS['db_pass'];
$database = $GLOBALS['db_name'];
}
/*****************************************************
** ?????????
*****************************************************/
if (!$this->db = @mysql_pconnect($server, $username, $password))
{
if ($username == 'root' && $database == 'siteengine')
{
@header("Location:/install.php");
}
else
{
$this->connectData=false;
//$this->error("database connect fail");
}
}
if ($database&&$this->connectData===true)
{
if($this->version() > '4.1')
{
global $config_charset;
$config_charset = 'utf-8';
if(!$dbcharset && in_array(strtolower($config_charset), array('gbk', 'big5', 'utf-8')))
{
$dbcharset = str_replace('-', '', $config_charset);
}
if($dbcharset)
{
mysql_query("SET NAMES '$dbcharset'");
}
}
if($this->version() > '5.0.1')
{
mysql_query("SET sql_mode=''");
}
$this->selectDb($database);
}
return true;
}
/*****************************************************
** ????????????
*****************************************************/
function disconnect()
{
$result = mysql_close() or $this->error();
return $result;
}
/*****************************************************
** ????????
*****************************************************/
function selectDb($database)
{
global $multidomain,$site_engine_root,$tablepre,$db;
if (@mysql_select_db($database, $this->db))
{
return true;
}
else
{
if ($multidomain==1)
{
$query = $this->query("CREATE DATABASE ".$database);
@header("Location:/install.php");
// require $site_engine_root.'lib/query.php';
// $fp = @fopen($site_engine_root.'data/sql/siteengine.sql', "r");
// $query=@fread($fp, filesize($site_engine_root.'data/sql/siteengine.sql'));
// @fclose($fp);
// if($tablepre!='boka') $query=str_replace("boka_",$tablepre,$query);
// $sql_query = new query($query);
// $sql_query->doquery();
// @header("Location:/index.php");
}
$this->error("$database database connect error!");
}
}
/*****************************************************
** ???????????Web?????????У????????????????С???Web?????·???true???????????·???false
*****************************************************/
function isWebEnv()
{
if (isset($_SERVER['PHP_SELF']))
{
return true;
}
if (isset($GLOBALS['_SERVER']['PHP_SELF']))
{
return true;
}
return false;
}
/*****************************************************
** ???????????????????
*****************************************************/
function _textConvert($text)
{
if ($this->isWebEnv()) // Web????
{
return nl2br($text);
}
return $text;
}
/*****************************************************
** ?????????
*****************************************************/
function currentTime()
{
list($usec, $sec) = explode(' ', microtime());
$time = (double)$sec + (double)$usec;
return $time;
}
function show()
{
$message = "\n" . 'Processed in '
. ($this->currentTime() - $this->seconds) . ' second(s), '
. $this->queries . ' queries.' . "\n";
// return $this->_textConvert($message);
return $message;
}
/*****************************************************
** ?????????????????????
*****************************************************/
function error($var = false)
{
global $domain,$language,$site_engine_root;
// header('Content-Type: text/html; charset="utf-8"');
// @$error = "
An error was encountered |
"; // @$error .="SiteEngine\n".$GLOBALS['l_site']['dberror']." error in file \"".$_SERVER['PHP_SELF']."\"\n"; // @$error .= $GLOBALS['l_site']['errorinfo'].mysql_errno() . ': ' . mysql_error() . "\n"; if ($var) { $error .= "\n" . htmlspecialchars(@$GLOBALS['l_site']['errorsql']) . "\n" . $var . "\n"; } $error .=" |
if ($GLOBALS['sesettings']['system']['errorreport']==1)
{
$errorinfo = mysql_errno().":".rawurlencode(mysql_error());
$time = date("Ymd H:i:s",time());
// @file("http://error.siteengine.net/?domain=$domain&errorinfo=$errorinfo&username=$SESSION[username]&time=$time&language=$language");
}
if (file_exists($site_engine_root.'data/install.lock'))
{
die($this->_textConvert($error));
}
else
{
die($this->_textConvert($error));
// @header("Location:/install.php");
}
// ??????
return $error;
}
/*****************************************************
** ???????sql????
*****************************************************/
function query($sql)
{
global $opendebug,$admincp;
$this->start_timer();
if (($opendebug==1 && $admincp==0) || ($opendebug==2 && $admincp==1))
{
echo ''.$sql.';
';
echo gettotaltime().'
';
// echo $sql.';
';
}
$result = @mysql_query($sql, $this->db) or $this->error($sql);
++$this->queries;// ??????????
$this->querytimes[] = $this->stop_timer();
$lowersql=strtolower($sql);
$lowersql=trim($lowersql);
if(substr($lowersql,0,6) == "update" or substr($lowersql,0,6) == "delete" )
{
//echo "find update";
$affectrows=mysql_affected_rows();
return $affectrows;
}
return $result;
}
/*****************************************************
** ???????sql????
*****************************************************/
function unbufferedQuery($sql)
{
$result = mysql_unbuffered_query($sql, $this->db) or $this->error($sql);
return $result;
}
/*****************************************************
** ????????????????????
*****************************************************/
/*
* $result ??query()????????
* $fetch_mode ????????Ч???
* DB_FETCHMODE_ORDERED (default) ??????????????????
* DB_FETCHMODE_ASSOC ???????????????????
* DB_FETCHMODE_OBJECT ?????????????
*/
function fetchRow($result, $fetch_mode = false)
{
//assert('is_resource($result)');
switch ($fetch_mode)
{
case DB_FETCHMODE_OBJECT:
$row = @mysql_fetch_object($result);//$row->username
break;
case DB_FETCHMODE_ASSOC:
$row = @mysql_fetch_assoc($result);//$row['username']
break;
case DB_FETCHMODE_ORDERED:
default: // ?????
$row = @mysql_fetch_row($result);// 0,1,2,3
break;
}
return $row;
}
}
?>
回复讨论(解决方案)
显然是你打开的编码与文件的实际编码并不一致。
复制出来的是没办法还原的。你要看原始文件,用正确的编码打开
如果用不正确的编码打开后又保存了,基本上就没办法恢复了。
通过编辑器新建文件 编码设置 utf-8 把你文件复制进去,就不乱码。
3楼你试过?你用的是什么编辑器。我新建了一个txt,然后把它设为UTF-8,再把代码放进去,依然乱码。
把你的原始文件放到云盘上
3楼你试过?你用的是什么编辑器。我新建了一个txt,然后把它设为UTF-8,再把代码放进去,依然乱码。
notepad++ 你试试这个编辑器
1.你这个文件可以在别人给你的时候,就已经是乱码了。
2.文件的存储方改成 UTF8,可以使用UE , EditPlus 都可以,直接另存为,编码选UTF8。
好像不可逆了。
推荐阅读
-
php 中文字符入库或显示乱码问题的解决方法_PHP
-
php递归复制文件和目录,该如何解决
-
php中json_encode UTF-8中文乱码的更好解决方法
-
跨浏览器下PHP下载文件名中的中文乱码问题的解决方法
-
php函数substr截取中文字符出现乱码的解决方法
-
jquery怎样输出php用file_get_contents() 获取的图片文件,该如何处理
-
php插入中文到sqlserver 2008里出现乱码的解决办法分享_PHP
-
求一利用php快速生成柱状图的函数,现以完成一个简易函数比较慢。该如何解决
-
解决PHP mail发送UTF-8中文邮件乱码
-
【实时项目进度贴】原创PHP社区论坛项目正在进行中!关注。o(∩_∩)o.哈哈。该如何解决