当前位置: 编程技术>php
本页文章导读:
▪自己动手做一个SQL解释器
自己动手做一个SQL解释器在一些小型的应用中,完全没有必要使用大型数据库软件。自己做一个SQL解释器就能用数据库的方式来管理了。这个解释器,能解释常用的SQL命令。你可以自行添加.........
▪GD输出汉字的函数的分析
很早以前找到一个把GB码转化为UTF-8的函数,配合一个GB到UNICODE的对照表(gb2312.txt),用于在GD中输出汉字。后来发现在欲输出的内容中含有西文字符时,会出现混乱。后来找到了修改.........
▪类的另类用法--数据的封装
类的另类用法--数据的封装一般的情况下,如果使用classname::property是不能访问到类的属性的,但可以用classname::method()使用类的方法。同样的也不能用objectname->property访问到类的方法里的变.........
[1]自己动手做一个SQL解释器
来源: 互联网 发布时间: 2013-11-30
自己动手做一个SQL解释器
在一些小型的应用中,完全没有必要使用大型数据库软件。自己做一个SQL解释器就能用数据库的方式来管理了。
这个解释器,能解释常用的SQL命令。你可以自行添加其他功能。
<?php
class DB_text {
var $conn;
var $classname = "db_text";
var $database;
function on_create() {
}
function connect($database_name) {
$this->database = $database_name;
if(! file_exists($database_name)) {
$this->conn = array();
$this->_close();
}
$fp = fopen($this->database,"r");
$this->conn = unserialize(fread($fp,filesize($this->database)));
fclose($fp);
}
function &query($query) {
if(eregi("select ",$query)) return $this->_select($query);
if(eregi("insert ",$query)) return $this->_insert($query);
if(eregi("delete ",$query)) return $this->_delete($query);
if(eregi("update ",$query)) return $this->_update($query);
return array();
}
function fetch_row(&$result) {
if(list($key,$value) = each($result))
return $value;
return false;
}
function num_rows($result) {
return count($result);
}
/**
* query的辅助函数
*/
function _select($query) {
if(eregi("(order by (.+))",$query,$regs)) {
$order = $regs[2];
$query = eregi_replace($regs[1],"",$query);
}
if(eregi("(group by (.+))",$query,$regs)) {
$group = $regs[2];
$query = eregi_replace($regs[1],"",$query);
}
eregi("select .* from ([0-9a-z_]+) *(where +(.+))?",$query,$regs);
if($regs[3] != "") {
$keys = $this->_where($regs[3],"\$this->conn[$regs[1]]");
while(list($key,$value) = each($keys)) {
$rs[] = $this->conn[$regs[1]][$value];
}
}else {
$rs = $this->conn[$regs[1]];
}
if($order) {
sscanf($order,"%s %s",$key,$type);
if(empty($type)) $type = "asc";
$this->_sort($rs,$key,$type);
}
return $rs;
}
function _insert($query) {
eregi("insert +into +([0-9a-z_]+) *(.+) *values? *(.+)",$query,$regs);
eval("\$key=array$regs[2];");
eval("\$value=array$regs[3];");
for($i=0;$i<count($key);$i++)
$rs[$key[$i]] = $value[$i];
$this->conn[$regs[1]][] = $rs;
$this->_close();
}
function _update($query) {
eregi("update +([0-9a-z_]+) +set *(,?.*=.*)+( +where +(.+))",$query,$regs);
$regs[2] = eregi_replace(",","=",$regs[2]);
$v = split("=",$regs[2]);
$keys = $this->_where($regs[4],"\$this->conn[$regs[1]]");
while(list($key,$value) = each($keys)) {
for($i=0;$i<count($v);$i+=2)
$this->conn[$regs[1]][$value][$v[$i]] = eregi_replace("'","",$v[$i+1]);
}
$this->_close();
}
function _delete($query) {
eregi("delete +from +([0-9a-z_]+) *(where +(.+))?",$query,$regs);
$keys = $this->_where($regs[3],"\$this->conn[$regs[1]]");
while(list($key,$value) = each($keys)) {
unset($this->conn[$regs[1]][$value]);
}
reset($this->conn[$regs[1]]);
while(list($key,$value) = each($this->conn[$regs[1]])) {
$ch[] = $value;
}
$this->conn[$regs[1]] = $ch;
$this->_close();
}
function _where($search,$table) {
$search = eregi_replace("\("," ( ",$search);
$search = eregi_replace("\)"," ) ",$search);
$search = eregi_replace("\+"," + ",$search);
$search = eregi_replace("\*"," * ",$search);
while(eregi("[^ ]([*/><!=-])",$search,$regs)) {
$search = eregi_replace($regs[1]," $regs[1] ",$search);
}
while(eregi("([><!] +=)",$search,$regs)) {
$search = eregi_replace($regs[1],eregi_replace(" ","",$regs[1]),$search);
}
$search = eregi_replace(" "," ",trim($search));
$search = eregi_replace(" and "," && ",$search);
$search = eregi_replace(" or "," || ",$search);
$search = eregi_replace(" = "," == ",$search);
$ar = split(" ",$search);
eval("\$t=$table;");
for($i=0;$i<count($ar);$i++) {
if(isset($t[0][$ar[$i]]))
$ar[$i] = "\$value[".$ar][$i]."]";
}
$expr = "\$expl=(".join(" ",$ar).");";
while(list($key,$value) = each($t)) {
eval($expr);
if($expl)
$keys[] = $key;
}
return $keys;
}
function _sort(&$ar,$key=0,$mode="desc") {
global $cmp_key;
$cmp_key = $key;
if($mode == "asc")
usort($ar,_cmp_asc);
else
usort($ar,_cmp_desc);
}
function _close() {
$fp = fopen($this->database,"w");
fwrite($fp,serialize($this->conn));
fclose($fp);
}
}
/** 排序键
*/
$cmp_key = "";
/** 排序用工作函数(降序 由usort()调用)
*/
function _cmp_desc($a,$b) {
global $cmp_key;
if ($a[$cmp_key] == $b[$cmp_key]) return 0;
return ($a[$cmp_key] > $b[$cmp_key]) ? -1 : 1;
}
/** 排序用工作函数(升序 由usort()调用)
*/
function _cmp_asc($a,$b) {
global $cmp_key;
if ($a[$cmp_key] == $b[$cmp_key]) return 0;
return ($a[$cmp_key] > $b[$cmp_key]) ? 1 : -1;
}
?>
测试例:
<pre>
<?php
//require_once "db_text.php";
$conn = new DB_text;
$conn->connect("text1.txt");
$conn->query("insert into manage (id,title) values (10,'abcd')");
$conn->query("insert into manage (id,title) values (2,'43d')");
$conn->query("insert into manage (id,title) values (20,'tuu')");
$conn->query("update manage set id=101,test='a' where id=10");
//$conn->query("delete from manage where id='10'");
//$conn->query("delete from manage where id=10 or table='code'");
//$rt = $conn->query("select * from manage where id=101 or table='code' group by 1 order by 1 asc");
$rt = $conn->query("select * from manage group by 1 order by id desc");
print_r($rt);
?>
</pre>
在一些小型的应用中,完全没有必要使用大型数据库软件。自己做一个SQL解释器就能用数据库的方式来管理了。
这个解释器,能解释常用的SQL命令。你可以自行添加其他功能。
<?php
class DB_text {
var $conn;
var $classname = "db_text";
var $database;
function on_create() {
}
function connect($database_name) {
$this->database = $database_name;
if(! file_exists($database_name)) {
$this->conn = array();
$this->_close();
}
$fp = fopen($this->database,"r");
$this->conn = unserialize(fread($fp,filesize($this->database)));
fclose($fp);
}
function &query($query) {
if(eregi("select ",$query)) return $this->_select($query);
if(eregi("insert ",$query)) return $this->_insert($query);
if(eregi("delete ",$query)) return $this->_delete($query);
if(eregi("update ",$query)) return $this->_update($query);
return array();
}
function fetch_row(&$result) {
if(list($key,$value) = each($result))
return $value;
return false;
}
function num_rows($result) {
return count($result);
}
/**
* query的辅助函数
*/
function _select($query) {
if(eregi("(order by (.+))",$query,$regs)) {
$order = $regs[2];
$query = eregi_replace($regs[1],"",$query);
}
if(eregi("(group by (.+))",$query,$regs)) {
$group = $regs[2];
$query = eregi_replace($regs[1],"",$query);
}
eregi("select .* from ([0-9a-z_]+) *(where +(.+))?",$query,$regs);
if($regs[3] != "") {
$keys = $this->_where($regs[3],"\$this->conn[$regs[1]]");
while(list($key,$value) = each($keys)) {
$rs[] = $this->conn[$regs[1]][$value];
}
}else {
$rs = $this->conn[$regs[1]];
}
if($order) {
sscanf($order,"%s %s",$key,$type);
if(empty($type)) $type = "asc";
$this->_sort($rs,$key,$type);
}
return $rs;
}
function _insert($query) {
eregi("insert +into +([0-9a-z_]+) *(.+) *values? *(.+)",$query,$regs);
eval("\$key=array$regs[2];");
eval("\$value=array$regs[3];");
for($i=0;$i<count($key);$i++)
$rs[$key[$i]] = $value[$i];
$this->conn[$regs[1]][] = $rs;
$this->_close();
}
function _update($query) {
eregi("update +([0-9a-z_]+) +set *(,?.*=.*)+( +where +(.+))",$query,$regs);
$regs[2] = eregi_replace(",","=",$regs[2]);
$v = split("=",$regs[2]);
$keys = $this->_where($regs[4],"\$this->conn[$regs[1]]");
while(list($key,$value) = each($keys)) {
for($i=0;$i<count($v);$i+=2)
$this->conn[$regs[1]][$value][$v[$i]] = eregi_replace("'","",$v[$i+1]);
}
$this->_close();
}
function _delete($query) {
eregi("delete +from +([0-9a-z_]+) *(where +(.+))?",$query,$regs);
$keys = $this->_where($regs[3],"\$this->conn[$regs[1]]");
while(list($key,$value) = each($keys)) {
unset($this->conn[$regs[1]][$value]);
}
reset($this->conn[$regs[1]]);
while(list($key,$value) = each($this->conn[$regs[1]])) {
$ch[] = $value;
}
$this->conn[$regs[1]] = $ch;
$this->_close();
}
function _where($search,$table) {
$search = eregi_replace("\("," ( ",$search);
$search = eregi_replace("\)"," ) ",$search);
$search = eregi_replace("\+"," + ",$search);
$search = eregi_replace("\*"," * ",$search);
while(eregi("[^ ]([*/><!=-])",$search,$regs)) {
$search = eregi_replace($regs[1]," $regs[1] ",$search);
}
while(eregi("([><!] +=)",$search,$regs)) {
$search = eregi_replace($regs[1],eregi_replace(" ","",$regs[1]),$search);
}
$search = eregi_replace(" "," ",trim($search));
$search = eregi_replace(" and "," && ",$search);
$search = eregi_replace(" or "," || ",$search);
$search = eregi_replace(" = "," == ",$search);
$ar = split(" ",$search);
eval("\$t=$table;");
for($i=0;$i<count($ar);$i++) {
if(isset($t[0][$ar[$i]]))
$ar[$i] = "\$value[".$ar][$i]."]";
}
$expr = "\$expl=(".join(" ",$ar).");";
while(list($key,$value) = each($t)) {
eval($expr);
if($expl)
$keys[] = $key;
}
return $keys;
}
function _sort(&$ar,$key=0,$mode="desc") {
global $cmp_key;
$cmp_key = $key;
if($mode == "asc")
usort($ar,_cmp_asc);
else
usort($ar,_cmp_desc);
}
function _close() {
$fp = fopen($this->database,"w");
fwrite($fp,serialize($this->conn));
fclose($fp);
}
}
/** 排序键
*/
$cmp_key = "";
/** 排序用工作函数(降序 由usort()调用)
*/
function _cmp_desc($a,$b) {
global $cmp_key;
if ($a[$cmp_key] == $b[$cmp_key]) return 0;
return ($a[$cmp_key] > $b[$cmp_key]) ? -1 : 1;
}
/** 排序用工作函数(升序 由usort()调用)
*/
function _cmp_asc($a,$b) {
global $cmp_key;
if ($a[$cmp_key] == $b[$cmp_key]) return 0;
return ($a[$cmp_key] > $b[$cmp_key]) ? 1 : -1;
}
?>
测试例:
<pre>
<?php
//require_once "db_text.php";
$conn = new DB_text;
$conn->connect("text1.txt");
$conn->query("insert into manage (id,title) values (10,'abcd')");
$conn->query("insert into manage (id,title) values (2,'43d')");
$conn->query("insert into manage (id,title) values (20,'tuu')");
$conn->query("update manage set id=101,test='a' where id=10");
//$conn->query("delete from manage where id='10'");
//$conn->query("delete from manage where id=10 or table='code'");
//$rt = $conn->query("select * from manage where id=101 or table='code' group by 1 order by 1 asc");
$rt = $conn->query("select * from manage group by 1 order by id desc");
print_r($rt);
?>
</pre>
[2]GD输出汉字的函数的分析
来源: 互联网 发布时间: 2013-11-30
很早以前找到一个把GB码转化为UTF-8的函数,配合一个GB到UNICODE的对照表(gb2312.txt),用于在GD中输出汉字。后来发现在欲输出的内容中含有西文字符时,会出现混乱。后来找到了修改后的代码,解决了问题。现将两个函数做一对比分析如下。
首先,这是一个UNICODE到UTF-8编码转换的函数,这一部分修改前后没有变化:
function u2utf8($c)
{
for($i=0;$i<count($c);$i++)
$str="";
if ($c < 0x80) {
$str.=$c;
}
else if ($c < 0x800) {
$str.=(0xC0 | $c>>6);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x10000) {
$str.=(0xE0 | $c>>12);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x200000) {
$str.=(0xF0 | $c>>18);
$str.=(0x80 | $c>>12 & 0x3F);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
return $str;
}
这里完全是按照UTF-8编码的规则,通过判断字符属于不同的UNICODE编码段范围,进行不同的移位和位与操作,以转化为UTF-8编码。关于该规则可参考http://www.utf8.org/上的说明。
这是修改前的GB转化为UTF-8编码的函数,其中调用了上面的u2utf8函数。
function gb2utf8($gb) /* Program writen by sadly www.phpx.com */
{
if(!trim($gb))
return $gb;
$filename="gb2312.txt";
$tmp=file($filename);
$codetable=array();
while(list($key,$value)=each($tmp))
$codetable[hexdec(substr($value,0,6))]=substr($value,7,6);
$utf8="";
while($gb)
{
if (ord(substr($gb,0,1))>127)
{
$this=substr($gb,0,2);
$gb=substr($gb,2,strlen($gb));
$utf8.=u2utf8(hexdec($codetable[hexdec(bin2hex($this))-0x8080]));
}
else
{
$gb=substr($gb,1,strlen($gb));
$utf8.=u2utf8(substr($gb,0,1));
}
}
$ret="";
for($i=0;$i<strlen($utf8);$i+=3)
$ret.=chr(substr($utf8,$i,3));
return $ret;
}
函数中while循环部分,把汉字逐个按照“对照表”转化为UNICODE,再通过u2utf8函数转化为UTF-8。但从中可以看出,while循环结束后,又用一个for循环,把每三个字节合成了一个UTF-8字符(见http://www.utf8.org/上的规则说明,每个汉字的UTF-8编码为三字节),没有考虑到其中的西文字符(西文字符的UTF-8编码为一字节)。所以,如果欲输出的内容中不论是开始时出现西文字符,或是汉字当中穿插西文字符,转化为UTF-8后,都会被按照“每三个字节截取”的方式截开,导致乱码。
以下是修改后的函数:
function gb2utf8($gb) /* Program writen by sadly modified by agun */
{
if(!trim($gb))
return $gb;
$filename="gb2312.txt";
$tmp=file($filename);
$codetable=array();
while(list($key,$value)=each($tmp))
$codetable[hexdec(substr($value,0,6))]=substr($value,7,6);
$ret="";
$utf8="";
while($gb)
{
if (ord(substr($gb,0,1))>127)
{
$this=substr($gb,0,2);
$gb=substr($gb,2,strlen($gb));
$utf8=u2utf8(hexdec($codetable[hexdec(bin2hex($this))-0x8080]));
for($i=0;$i<strlen($utf8);$i+=3)
$ret.=chr(substr($utf8,$i,3));
}
else
{
$ret.=substr($gb,0,1);
$gb=substr($gb,1,strlen($gb));
}
}
return $ret;
}
修改后的函数将 GB转化为UNICODE、UNICODE转化为UTF-8、几个字节合成一个UTF-8字符,这三个步骤在一个循环里完成,尤其是几个字节合成一个UTF-8字符这一步骤,放在判断了字符属于西文还是属于汉字的条件分支里,据此决定截取一个字节还是三个字节。于是结果正确了!
首先,这是一个UNICODE到UTF-8编码转换的函数,这一部分修改前后没有变化:
function u2utf8($c)
{
for($i=0;$i<count($c);$i++)
$str="";
if ($c < 0x80) {
$str.=$c;
}
else if ($c < 0x800) {
$str.=(0xC0 | $c>>6);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x10000) {
$str.=(0xE0 | $c>>12);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x200000) {
$str.=(0xF0 | $c>>18);
$str.=(0x80 | $c>>12 & 0x3F);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
return $str;
}
这里完全是按照UTF-8编码的规则,通过判断字符属于不同的UNICODE编码段范围,进行不同的移位和位与操作,以转化为UTF-8编码。关于该规则可参考http://www.utf8.org/上的说明。
这是修改前的GB转化为UTF-8编码的函数,其中调用了上面的u2utf8函数。
function gb2utf8($gb) /* Program writen by sadly www.phpx.com */
{
if(!trim($gb))
return $gb;
$filename="gb2312.txt";
$tmp=file($filename);
$codetable=array();
while(list($key,$value)=each($tmp))
$codetable[hexdec(substr($value,0,6))]=substr($value,7,6);
$utf8="";
while($gb)
{
if (ord(substr($gb,0,1))>127)
{
$this=substr($gb,0,2);
$gb=substr($gb,2,strlen($gb));
$utf8.=u2utf8(hexdec($codetable[hexdec(bin2hex($this))-0x8080]));
}
else
{
$gb=substr($gb,1,strlen($gb));
$utf8.=u2utf8(substr($gb,0,1));
}
}
$ret="";
for($i=0;$i<strlen($utf8);$i+=3)
$ret.=chr(substr($utf8,$i,3));
return $ret;
}
函数中while循环部分,把汉字逐个按照“对照表”转化为UNICODE,再通过u2utf8函数转化为UTF-8。但从中可以看出,while循环结束后,又用一个for循环,把每三个字节合成了一个UTF-8字符(见http://www.utf8.org/上的规则说明,每个汉字的UTF-8编码为三字节),没有考虑到其中的西文字符(西文字符的UTF-8编码为一字节)。所以,如果欲输出的内容中不论是开始时出现西文字符,或是汉字当中穿插西文字符,转化为UTF-8后,都会被按照“每三个字节截取”的方式截开,导致乱码。
以下是修改后的函数:
function gb2utf8($gb) /* Program writen by sadly modified by agun */
{
if(!trim($gb))
return $gb;
$filename="gb2312.txt";
$tmp=file($filename);
$codetable=array();
while(list($key,$value)=each($tmp))
$codetable[hexdec(substr($value,0,6))]=substr($value,7,6);
$ret="";
$utf8="";
while($gb)
{
if (ord(substr($gb,0,1))>127)
{
$this=substr($gb,0,2);
$gb=substr($gb,2,strlen($gb));
$utf8=u2utf8(hexdec($codetable[hexdec(bin2hex($this))-0x8080]));
for($i=0;$i<strlen($utf8);$i+=3)
$ret.=chr(substr($utf8,$i,3));
}
else
{
$ret.=substr($gb,0,1);
$gb=substr($gb,1,strlen($gb));
}
}
return $ret;
}
修改后的函数将 GB转化为UNICODE、UNICODE转化为UTF-8、几个字节合成一个UTF-8字符,这三个步骤在一个循环里完成,尤其是几个字节合成一个UTF-8字符这一步骤,放在判断了字符属于西文还是属于汉字的条件分支里,据此决定截取一个字节还是三个字节。于是结果正确了!
[3]类的另类用法--数据的封装
来源: 互联网 发布时间: 2013-11-30
类的另类用法--数据的封装
一般的情况下,如果使用classname::property是不能访问到类的属性的,但可以用classname::method()使用类的方法。同样的也不能用objectname->property访问到类的方法里的变量。利用这一特点,我们可以将一些数据保存于类中,有点象c++的私有属性。
<?
class data {
function value($var) {
static $d = array();
if(func_num_args() > 1) {
$d[$var] = func_get_arg(1);
}else {
return $d[$var];
}
}
}
//测试:
data::value("a",1);
data::value("b",2);
echo data::value("a");
echo data::value("b");
?>
一般的情况下,如果使用classname::property是不能访问到类的属性的,但可以用classname::method()使用类的方法。同样的也不能用objectname->property访问到类的方法里的变量。利用这一特点,我们可以将一些数据保存于类中,有点象c++的私有属性。
<?
class data {
function value($var) {
static $d = array();
if(func_num_args() > 1) {
$d[$var] = func_get_arg(1);
}else {
return $d[$var];
}
}
}
//测试:
data::value("a",1);
data::value("b",2);
echo data::value("a");
echo data::value("b");
?>
最新技术文章: