当前位置: 编程技术>php
本页文章导读:
▪php转换xml为数组的代码分享 以下代码,实现将xml转换为数组的功能。
例子:
<?php
/**
* 转换xml为数组
* edit by www.
*/
class xml {
private $parser;
private $tag_cur=0;
private $data=array();
private $struct=array();
.........
▪php中memcache缓存类的代码分享 代码1,操作memcache服务器。
<?php
class mem{
private static $conn;
public function mem($host='127.0.0.1',$port=11211)
{
$conn = new Memcache;
$conn->pconnect($host, $port);
}
.........
▪php判断用户是否掉线及关闭网页的方法分享 要实现判断用户已掉线并关闭网页,主要用到方法connection_status 和 connection_aborted。
通过一个例子,来了解下它们的用法:
<?
echo str_repeat(" ",300);
//以下不可省略,否则用户断线,php(线程).........
[1]php转换xml为数组的代码分享
来源: 互联网 发布时间: 2013-12-24
以下代码,实现将xml转换为数组的功能。
例子:
<?php /** * 转换xml为数组 * edit by www. */ class xml { private $parser; private $tag_cur=0; private $data=array(); private $struct=array(); function xml() { $this->parser = xml_parser_create(); xml_set_object($this->parser,&$this); xml_set_element_handler($this->parser,"tag_open","tag_close"); xml_set_character_data_handler($this->parser,"cdata"); } function parse($data) { $this->data=array(); $this->struct=array(); $this->tag_cur=0; xml_parse($this->parser,$data); return $this->data; } function tag_open($parser,$tag,$attributes) { $this->struct[]=$tag; $this->tag_cur++; } function cdata($parser,$cdata) { $tmp=&$this->data; for($i=0;$i<$this->tag_cur;$i++) { if(!isset($tmp[$this->struct[$i]])) { $tmp[$this->struct[$i]]=array(); } $tmp=&$tmp[$this->struct[$i]]; } if(!empty($tmp)) { $tmp1=$tmp; if(is_array($tmp1)) { $tmp=array_merge($tmp1,array($cdata)); }else{ $tmp=array($tmp1,$cdata); } }else $tmp=$cdata; } function tag_close($parser,$tag) { array_pop($this->struct); $this->tag_cur--; } } $xml=new xml(); echo "<pre>"; print_r($xml->parse('<a><a1><b1>b1</b1><b2>b2</b2><b3><c1><d1>d1</d1> <d1>d1_2</d1><d1>d1_3</d1></c1></b3></a1><e1>1</e1></a>')); echo "</pre>"; ?>
说明:
也可以使用 simplexml_load_string函数轻松搞定。
[2]php中memcache缓存类的代码分享
来源: 互联网 发布时间: 2013-12-24
代码1,操作memcache服务器。
<?php class mem{ private static $conn; public function mem($host='127.0.0.1',$port=11211) { $conn = new Memcache; $conn->pconnect($host, $port); } public function get($key) { return $this->conn->get($key); } public function set($key,$value,$expire=259200) { return $this->conn->set($key,$value,0,$expire); } public function del($key) { return $this->conn->delete($key); } public function clearAll() { return $this->conn->flush(); } } ?>
代码2,从数据库取数据,如果取不到,则自动加载设定的表到内存,假设,每个表的主键字段是id。
<?php //memcache缓存类 //by www. require_once('mem.php'); require_once('common.fun.php'); class cache{ private static $mem; private static $fixedTables=array( "enemy", "equip", "soldier_skill" ); private static $fixedTablesHasCol2Search=array( "soldier"=>array("soldier_id") ); private $db; public function cache($host='127.0.0.1',$port=11211,&$db) { self::$mem = new mem($host,$port); $this->db=$db; } public function get($tb,$pkv) { $return = self::$mem->get($tb.'_'.$pkv); if(!$return) { $this->loadAll(); $return = self::$mem->get($tb.'_'.$pkv); } if(!$return) common::dolog('cache class get "'.$tb.'_'.$pkv.'" failed'); return $return; } public function getByCol($tb,$col,$kv) { $return = self::$mem->get($tb.'_'.$col.'_'.$kv); if(!$return) { $this->loadAll(); $return = self::$mem->get($tb.'_'.$col.'_'.$kv); } if(!$return) common::dolog('cache class getByCol "'.$tb.'_'.$col.'_'.$kv.'" failed'); return $return; } public function loadAll() { foreach(self::$fixedTables as $tb) { $rows=$this->db->getRows('select '.chr(42).' from '.$tb); $seachCol=array(); $searhColData=array(); if(isset(self::$fixedTablesHasCol2Search[$tb])) { $seachCol=self::$fixedTablesHasCol2Search[$tb]; } foreach($rows as $row) { self::$mem->set($tb.'_'.$row['id'],$row); if(!empty($seachCol)) { foreach($seachCol as $col) { $searhColData[$tb.'_'.$col.'_'.$row[$col]][]=$row; } } } foreach($searhColData as $k=>$v) { self::$mem->set($k,$v); } } } public function set($key,$value,$expire=259200) { return self::$mem->set($key,$value,0,$expire); } public function del($key) { return self::$mem->del($key); } } ?>
[3]php判断用户是否掉线及关闭网页的方法分享
来源: 互联网 发布时间: 2013-12-24
要实现判断用户已掉线并关闭网页,主要用到方法connection_status 和 connection_aborted。
通过一个例子,来了解下它们的用法:
<? echo str_repeat(" ",300); //以下不可省略,否则用户断线,php(线程)立即终止,不会执行" if connection_status()!=0||connection_aborted){" ignore_user_abort(true); while (true) { echo "test<br>/n;//必须有输出, 否则线程会一直执行下去,直到重新启动apche(测试过程2小时),输出也可以写到//注释2处 flush(); if (connection_status()!=0||connection_aborted()){ //用户退出了 } //注释2 sleep(2); //by www. } ?>
下面是另一个例子:
<? //检测是否掉线,然后关闭网页 //编辑:www. echo str_repeat(" ",300); ignore_user_abort(true); //without this, current apache thread will terminate Immediately,so the code "if (connection_status()!=0){" will not be executed as the script was broken off! while (true) { echo "test<br>/n";//if there's no any output, this script will execute endless, which means the current apache thread will not end until you restart apache and connection_status()will keep 0 and connection_aborted() will keep false. flush(); sleep(2); if (connection_status()!=0){ include ('dbconnect.inc'); $sql="delete from online_users where online_user=$user"; $sql_exec=pg_exec($vChatDB, $sql); die(); //kills the script } } ?>
最新技术文章: