龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php 一个很简单的文本式留言板

时间:2014-07-08 15:01来源:网络整理 作者:网络 点击:
分享到:
一个很简单的文本式留言板 [代码片段(52行)]
<?php
    /**
    * 留言板类
    */
    class message{

        public static $file = './message.txt';   //文件地址

        /**
        *  写入
        *
        * param array data 要保存的信息(可以是二维数组)
        */
        public static function put($data){
            if(empty($data)) return false;
            $text = self::setArray($data);
            file_put_contents(self::$file,$text);
        }
        /**
        *  获取
        *
        * return array
        */
        public static function get(){
            $file = file_get_contents(self::$file);
            eval("\\$return = {$file};");
            return $return;
        }
        /*
        * 将数组变成字符串形式
        *
        * return string
        */
        protected static function setArray($arr){
            $return = 'array(';
            foreach($arr as $key=>$val){
                if(is_array($val)){
                    $return .= "'{$key}'=>".self::setArray($val);
                }else{
                    $return .= "'{$key}'=>'{$val}',";
                }           
            }
            $return = trim($return,',');
            $return .= ')';
            return $return;
        }
    }
    $data = message::get(); //假设取出的数据
    $data[] = array(1,2,3,4);   //增加一条数据
    message::put($data);    //必须得是一次取出的数据,否则会将原来的覆盖写
?>
//该片段来自于http://outofmemory.cn
精彩图集

赞助商链接