php生成zip压缩文件两个实例详解

 更新时间:2016年11月25日 16:20  点击:2049
在php中生成zip文件我们只要使用一个php zip压缩ZipArchive函数就可以了,下面小编来给大家总结两个实现一个是利用ZipArchive生成zip,另一个压缩文件夹下所有文件。

注意:

ZipArchive来压缩文件。这个是php的扩展类,自php5.2版本以后就已经支持这个扩展,如果你在使用的时候出现错误,查看下php.ini里面的extension=php_zip.dll前面的分号有没有去掉,然后再重启Apache这样才能使用这个类库。

例1

生成zip 压缩文件

 代码如下 复制代码

<?php
/* 生成zip 压缩文件 */
function create_zip($files = array(),$destination = '',$overwrite = false) {
    //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }
    //vars
    $valid_files = array();
    //if files were passed in...
    if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
            //make sure the file exists
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }
    //if we have good files...
    if(count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }
        //add the files
        foreach($valid_files as $file) {
            $file_info_arr= pathinfo($file);
            $zip->addFile($file,$file_info_arr['basename']);//去掉层级目录
        }
        //debug
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
 
        //close the zip -- done!
        $zip->close();
 
        //check to make sure the file exists
        return file_exists($destination);
    }
    else
    {
        return false;
    }
}
 
define('ROOTPATH',dirname ( __FILE__ )); //网站路径
 
$files_to_zip = array(
    ROOTPATH.DIRECTORY_SEPARATOR.'PHP+jQuery+Cookbook.pdf',
    ROOTPATH.DIRECTORY_SEPARATOR.'TurboListerZeroTemplate.csv'
);
//if true, good; if false, zip creation failed
$filename='my-archive.zip';
$result = create_zip($files_to_zip,$filename);

例2

压缩文件夹下面的所有文

 代码如下 复制代码

<?php
/*
php zip压缩文件夹下面的所有文件
*/
class HZip
{
  /**
   * 添加文件和子目录的文件到zip文件
   * @param string $folder
   * @param ZipArchive $zipFile
   * @param int $exclusiveLength Number of text to be exclusived from the file path.
   */
  private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
    $handle = opendir($folder);
    while (false !== $f = readdir($handle)) {
      if ($f != '.' && $f != '..') {
        $filePath = "$folder/$f";
        // Remove prefix from file path before add to zip.
        $localPath = substr($filePath, $exclusiveLength);
        if (is_file($filePath)) {
          $zipFile->addFile($filePath, $localPath);
        } elseif (is_dir($filePath)) {
          // 添加子文件夹
          $zipFile->addEmptyDir($localPath);
          self::folderToZip($filePath, $zipFile, $exclusiveLength);
        }
      }
    }
    closedir($handle);
  }
 
  /**
   * Zip a folder (include itself).
   * Usage:
   *   HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip');
   *
   * @param string $sourcePath Path of directory to be zip.
   * @param string $outZipPath Path of output zip file.
   */
  public static function zipDir($sourcePath, $outZipPath)
  {
    $pathInfo = pathInfo($sourcePath);
    $parentPath = $pathInfo['dirname'];
    $dirName = $pathInfo['basename'];
    $sourcePath=$parentPath.'/'.$dirName;//防止传递'folder' 文件夹产生bug
    $z = new ZipArchive();
    $z->open($outZipPath, ZIPARCHIVE::CREATE);//建立zip文件
    $z->addEmptyDir($dirName);//建立文件夹
    self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
    $z->close();
  }
}
 
//使用方法
HZip::zipDir('yourlife', 'yourlife.zip');
?>

/******** ziparchive 可选参数 *******/
/*
1.ZipArchive::addEmptyDir

添加一个新的文件目录

2.ZipArchive::addFile

将文件添加到指定zip压缩包中。

3.ZipArchive::addFromString

添加的文件同时将内容添加进去

4.ZipArchive::close

关闭ziparchive

5.ZipArchive::extractTo

将压缩包解压

6.ZipArchive::open

打开一个zip压缩包

7.ZipArchive::getStatusString

返回压缩时的状态内容,包括错误信息,压缩信息等等

8.ZipArchive::deleteIndex

删除压缩包中的某一个文件,如:deleteIndex(0)删除第一个文件

9.ZipArchive::deleteName

删除压缩包中的某一个文件名称,同时也将文件删除。
......
*/

缓存类是我们开发应用中会常用使用到的功能,下面我来给大家整理几个php文件缓存类了,各个文件缓存类写法不同,但在性能上会有区别,有兴趣测试的朋友可测试一下这些缓存类吧。

例1

 代码如下 复制代码

<?php
$fzz = new fzz_cache;
$fzz->kk = $_SERVER; //写入缓存
//$fzz->set("kk",$_SERVER,10000); //此方法不与类属性想冲突,可以用任意缓存名;
print_r($fzz->kk);  //读取缓存
//print_r($fzz->get("kk"));
//unset($fzz->kk); //删除缓存
//$fzz->_unset("kk");
var_dump(isset($fzz->kk)); //判断缓存是否存在
//$fzz->_isset("kk");
//$fzz->clear(); //清理过期缓存
//$fzz->clear_all(); //清理所有缓存文件
class fzz_cache{
 public $limit_time = 20000; //缓存过期时间
 public $cache_dir = "data"; //缓存文件保存目录

 //写入缓存
 function __set($key , $val){
  $this->_set($key ,$val);
 }
 //第三个参数为过期时间
 function _set($key ,$val,$limit_time=null){  
  $limit_time = $limit_time ? $limit_time : $this->limit_time;
  $file = $this->cache_dir."/".$key.".cache";
  $val = serialize($val);
  @file_put_contents($file,$val) or $this->error(__line__,"fail to write in file");
  @chmod($file,0777);
  @touch($file,time()+$limit_time) or $this->error(__line__,"fail to change time");
 }


 //读取缓存
 function __get($key){
  return $this->_get($key);
 }
 function _get($key){
  $file = $this->cache_dir."/".$key.".cache";
  if (@filemtime($file)>=time()){
   return unserialize(file_get_contents($file));
  }else{
   @unlink($file) or $this->error(__line__,"fail to unlink");
   return false;
  }
 }


 //删除缓存文件
 function __unset($key){
  return $this->_unset($key);
 }
 function _unset($key){
  if (@unlink($this->cache_dir."/".$key.".cache")){
   return true;
  }else{
   return false;
  }
 }


 //检查缓存是否存在,过期则认为不存在
 function __isset($key){
  return $this->_isset($key);
 }
 function _isset($key){
  $file = $this->cache_dir."/".$key.".cache";
  if (@filemtime($file)>=time()){
   return true;
  }else{
   @unlink($file) ;
   return false;
  }
 }


 //清除过期缓存文件
 function clear(){
  $files = scandir($this->cache_dir);
  foreach ($files as $val){
   if (filemtime($this->cache_dir."/".$val)<time()){
    @unlink($this->cache_dir."/".$val);
   }
  }
 }


 //清除所有缓存文件
 function clear_all(){
  $files = scandir($this->cache_dir);
  foreach ($files as $val){
   @unlink($this->cache_dir."/".$val);
  }
 }
 
 function error($msg,$debug = false) {
  $err = new Exception($msg);
  $str = "<pre>
<span style='color:red'>error:</span>
".print_r($err->getTrace(),1)."
</pre>";
  if($debug == true) {
   file_put_contents(date('Y-m-d H_i_s').".log",$str);
   return $str;
  }else{
   die($str);
  }
 }
}
?>

例2.从CI社区的stblog和CI的file_helper类中提取出来的php文件缓存类,一个简单的基于文件的key->value缓存类。

这个类可以用来缓存一些基本信息,比如博客的header,footer,sidebar中的一些不经常变化,从数据库中取出的

内容,取数据前先判断文件缓存中的内容是否过期,如果没过期取出来,过期了则连接数据库查询,并将结果重新

写入文件缓存,更新过期时间。跟memcache使用类似,不过更方便。用在一些小的应用上足够了...代码如下

 代码如下 复制代码

<?php
define('DIRECTORY_SEPARATOR','/');
define('FOPEN_WRITE_CREATE_DESTRUCTIVE','wb');
define('FOPEN_WRITE_CREATE','ab');
define('DIR_WRITE_MODE', 0777);
class FileCache {

 /**
     * 缓存路径
     *
     * @access private
     * @var string
     */
 private $_cache_path;

 /**
     * 缓存过期时间,单位是秒second
     *
     * @access private
     * @var int
     */
 private $_cache_expire;
 
 /**
     * 解析函数,设置缓存过期实践和存储路径
     *
     * @access public
     * @return void
     */
 public function __construct($expire, $cache_path)
 {
  $this->_cache_expire = $expire;
  $this->_cache_path = $cache_path;
 }
 
 /**
     * 缓存文件名
     *
     * @access public
     * @param  string $key
     * @return void
     */
 private function _file($key)
 {
  return $this->_cache_path . md5($key);
 }
 
 /**
     * 设置缓存
     *
     * @access public
     * @param  string $key 缓存的唯一键
     * @param  string $data 缓存的内容
     * @return bool
     */
 public function set($key, $data)
 {
  $value = serialize($data);
 
  $file = $this->_file($key);
 
     return $this->write_file($file, $value);
 }
 
 /**
     * 获取缓存
     *
     * @access public
     * @param  string $key 缓存的唯一键
     * @return mixed
     */
 public function get($key)
 {
  $file = $this->_file($key);
 
  /** 文件不存在或目录不可写 */
  if (!file_exists($file) || !$this->is_really_writable($file))
  {
   return false;
  }
 
  /** 缓存没有过期,仍然可用 */
  if ( time() < (filemtime($file) + $this->_cache_expire) )
  {
  
   $data = $this->read_file($file);
  
   if(FALSE !== $data)
   {
    return unserialize($data);
   }
  
   return FALSE;
  }
 
  /** 缓存过期,删除之 */
  @unlink($file);
  return FALSE;
  }
 
  function read_file($file)
 {
  if ( ! file_exists($file))
  {
   return FALSE;
  }
 
  if (function_exists('file_get_contents'))
  {
   return file_get_contents($file); 
  }

  if ( ! $fp = @fopen($file, FOPEN_READ))
  {
   return FALSE;
  }
 
  flock($fp, LOCK_SH);//读取之前加上共享锁
 
  $data = '';
  if (filesize($file) > 0)
  {
   $data =& fread($fp, filesize($file));
  }

  flock($fp, LOCK_UN);//释放锁
  fclose($fp);

  return $data;
 }
 
  function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
 {
  if ( ! $fp = @fopen($path, $mode))
  {
   return FALSE;
  }
 
  flock($fp, LOCK_EX);
  fwrite($fp, $data);
  flock($fp, LOCK_UN);
  fclose($fp);
 
  return TRUE;
 }
 function is_really_writable($file)//兼容各平台判断文件是否有写入权限
 {
  // If we're on a Unix server with safe_mode off we call is_writable
  if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
  {
   return is_writable($file);
  }
 
  // For windows servers and safe_mode "on" installations we'll actually
  // write a file then read it.  Bah...
  if (is_dir($file))
  {
   $file = rtrim($file, '/').'/'.md5(rand(1,100));
 
   if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
   {
    return FALSE;
   }
 
   fclose($fp);
   @chmod($file, DIR_WRITE_MODE);
   @unlink($file);
   return TRUE;
  }
  elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
  {
   return FALSE;
  }
 
  fclose($fp);
  return TRUE;
 }
}
$cache = new FileCache(30,'cache/');
$cache->set('test','this is a test.');
print $cache->get('test');
/* End of file FlieCache.php */


 

例3.自己觉得很好用的php文件缓存

 代码如下 复制代码

<?php
class cache

 private static $_instance = null;

    protected $_options = array(
        'cache_dir'        => "./",
        'file_name_prefix' => 'cache',
     'mode'            => '1', //mode 1 为serialize model 2为保存为可执行文件
    ); 
 
 /**
  * 得到本类实例
  *
  * @return Ambiguous
  */
 public static function getInstance()
 {
  if(self::$_instance === null)
  {
   self::$_instance = new self();
  }
  return self::$_instance;
 }
 
 /**
  * 得到缓存信息
  *
  * @param string $id
  * @return boolean|array
  */
 public static function get($id)
 {
  $instance = self::getInstance();
  
  //缓存文件不存在
  if(!$instance->has($id))
  {
   return false;
  }
  
  $file = $instance->_file($id);
  
  $data = $instance->_fileGetContents($file);
  
  if($data['expire'] == 0 || time() < $data['expire'])
  {
   return $data['contents'];
  }
  return false;
 }
 
 /**
  * 设置一个缓存
  *
  * @param string $id   缓存id
  * @param array  $data 缓存内容
  * @param int    $cacheLife 缓存生命 默认为0无限生命
  */
 public static function set($id, $data, $cacheLife = 0)
 {
  $instance = self::getInstance();
  
  $time = time();
  $cache      = array();
  $cache['contents'] = $data;
  $cache['expire']   = $cacheLife === 0 ? 0 : $time + $cacheLife;
  $cache['mtime']    = $time;
  
  $file = $instance->_file($id);
  
  return $instance->_filePutContents($file, $cache);
 }
 
    /**
     * 清除一条缓存
     *
     * @param string cache id 
     * @return void
     */  
 public static function delete($id)
 {
  $instance = self::getInstance();
  
  if(!$instance->has($id))
  {
   return false;
  }
     $file = $instance->_file($id);
     //删除该缓存
     return unlink($file);
 }
 
 /**
  * 判断缓存是否存在
  *
  * @param string $id cache_id
  * @return boolean true 缓存存在 false 缓存不存在
  */
 public static function has($id)
 {
  $instance = self::getInstance();
  $file     = $instance->_file($id);
  
  if(!is_file($file))
  {
   return false;
  }
  return true;
 }
 
 /**
  * 通过缓存id得到缓存信息路径
  * @param string $id
  * @return string 缓存文件路径
  */
 protected function _file($id)
 {
  $instance  = self::getInstance();
  $fileNmae  = $instance->_idToFileName($id);
  return $instance->_options['cache_dir'] . $fileNmae;
 } 
 
 /**
  * 通过id得到缓存信息存储文件名
  *
  * @param  $id
  * @return string 缓存文件名
  */
 protected function _idToFileName($id)
 {
  $instance  = self::getInstance();
  $prefix    = $instance->_options['file_name_prefix'];
  return $prefix . '---' . $id;
 }
 
 /**
  * 通过filename得到缓存id
  *
  * @param  $id
  * @return string 缓存id
  */
 protected function _fileNameToId($fileName)
 {
  $instance  = self::getInstance();
  $prefix    = $instance->_options['file_name_prefix'];
  return preg_replace('/^' . $prefix . '---(.*)$/', '$1', $fileName);
 }
 
 /**
  * 把数据写入文件
  *
  * @param string $file 文件名称
  * @param array  $contents 数据内容
  * @return bool
  */
 protected function _filePutContents($file, $contents)
 {
  if($this->_options['mode'] == 1)
  {
   $contents = serialize($contents);
  }
  else
  {
   $time = time(); 
         $contents = "<?phpn".
                 " // mktime: ". $time. "n".
                 " return ".
                 var_export($contents, true).
                 "n?>";
  }
  
  $result = false;
     $f = @fopen($file, 'w');
        if ($f) {
            @flock($f, LOCK_EX);
            fseek($f, 0);
            ftruncate($f, 0);
            $tmp = @fwrite($f, $contents);
            if (!($tmp === false)) {
                $result = true;
            }
            @fclose($f);
        }
  @chmod($file,0777);
  return $result;    
 }
 
 /**
  * 从文件得到数据
  *
  * @param  sring $file
  * @return boolean|array
  */
 protected function _fileGetContents($file)
 {
  if(!is_file($file))
  {
   return false;
  }
  
  if($this->_options['mode'] == 1)
  {
   $f = @fopen($file, 'r');
   @$data = fread($f,filesize($file));
   @fclose($f);
   return unserialize($data);
  }
  else
  {
   return include $file;
  }
 }
 
 /**
  * 构造函数
  */
 protected function __construct()
 {
 
 }
 
 /**
  * 设置缓存路径
  *
  * @param string $path
  * @return self
  */
 public static function setCacheDir($path)
 {
  $instance  = self::getInstance();
        if (!is_dir($path)) {
            exit('file_cache: ' . $path.' 不是一个有效路径 ');
        }
        if (!is_writable($path)) {
            exit('file_cache: 路径 "'.$path.'" 不可写');
        }
   
        $path = rtrim($path,'/') . '/';
        $instance->_options['cache_dir'] = $path;
       
        return $instance;
 }
 
 /**
  * 设置缓存文件前缀
  *
  * @param srting $prefix
  * @return self
  */
 public static function setCachePrefix($prefix)
 {
  $instance  = self::getInstance();
  $instance->_options['file_name_prefix'] = $prefix;
  return $instance;
 }
 
 /**
  * 设置缓存存储类型
  *
  * @param int $mode
  * @return self
  */
 public static function setCacheMode($mode = 1)
 {
  $instance  = self::getInstance();
  if($mode == 1)
  {
   $instance->_options['mode'] = 1;
  }
  else
  {
   $instance->_options['mode'] = 2;
  }
  
  return $instance;
 }
 
 /**
  * 删除所有缓存
  * @return boolean
  */
 public static function flush()
 {
  $instance  = self::getInstance();
  $glob = @glob($instance->_options['cache_dir'] . $instance->_options['file_name_prefix'] . '--*');
  
  if(empty($glob))
  {
   return false;
  }
  
  foreach ($glob as $v)
  {
   $fileName = basename($v);
   $id =  $instance->_fileNameToId($fileName);
   $instance->delete($id);
  }
  return true;
 }
}

/* 初始化设置cache的配置信息什么的 */
cache::setCachePrefix('core'); //设置缓存文件前缀
cache::setCacheDir('./cache'); //设置存放缓存文件夹路径

//模式1 缓存存储方式
//a:3:{s:8:"contents";a:7:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:34;i:4;i:5;i:5;i:6;i:6;i:6;}s:6:"expire";i:0;s:5:"mtime";i:1318218422;}
//模式2 缓存存储方式
/*
 <?php
 // mktime: 1318224645
 return array (
  'contents' =>
  array (
    0 => 1,
    1 => 2,
    2 => 3,
    3 => 34,
    4 => 5,
    5 => 6,
    6 => 6,
  ),
  'expire' => 0,
  'mtime' => 1318224645,
)
?>
 *
 *
 */
cache::setCacheMode('2');

if(!$row = cache::get('zj2'))
{
 
 $array = array(1,2,3,34,5,6,6);
 $row = cache::set('zj2',$array);
}
// cache::flush(); 清空所有缓存

print_r($row);
 
件缓存 class    

<?php
/**
 * 文件缓存类
 * @author  xiaojiong & 290747680@qq.com
 * @date 2011-08-17
 */
class cache
{
 const FILE_LIFE_KEY = 'FILE_LIFE_KEY';
 
 const CLEAR_ALL_KEY = 'CLEAR_ALL';
 
    static $_instance = null;
 
    protected $_options = array(
        'cache_dir' => './cache',
        'file_locking' => true,
        'file_name_prefix' => 'cache',
        'cache_file_umask' => 0777,
     'file_life'  => 100000
    );
   
    static function &getInstance($options = array())
    {
     if(self::$_instance === null)
     {
      self::$_instance = new self($options);
     } 
     return self::$_instance;
    }
   
    /**
     * 设置参数
     * @param  array $options 缓存参数
     * @return void
     */
 static function &setOptions($options = array())
 {
         return self::getInstance($options);
 }
 
    /**
     * 构造函数
     * @param  array $options 缓存参数
     * @return void
     */
    private function __construct($options = array())
    {  
        if ($this->_options['cache_dir'] !== null) {
   
   $dir = rtrim($this->_options['cache_dir'],'/') . '/';
         $this->_options['cache_dir'] = $dir;
        
   if (!is_dir($this->_options['cache_dir'])) {
             mkdir($this->_options['cache_dir'],0777,TRUE);
         }
         if (!is_writable($this->_options['cache_dir'])) {
             exit('file_cache: 路径 "'. $this->_options['cache_dir'] .'" 不可写');
         }
     
        } else {
           exit('file_cache: "options" cache_dir 不能为空 ');
        }
    }

    /**
     * 设置缓存路径
     * @param  string  $value
     * @return void
     */
    static function setCacheDir($value)
    {
     $self = & self::getInstance();
     
        if (!is_dir($value)) {
            exit('file_cache: ' . $value.' 不是一个有效路径 ');
        }
        if (!is_writable($value)) {
            exit('file_cache: 路径 "'.$value.'" 不可写');
        }
   
        $value = rtrim($this->_options['cache_dir'],'/') . '/';
       
        $self->_options['cache_dir'] = $value;
    }
   
    /**
     * 存入缓存数据
     * @param  array  $data          放入缓存的数据
     * @param  string $id            缓存id(又名缓存识别码)
     * @param  cache_life            缓存时间
     * @return boolean True if no problem
     */
    static function save($data, $id = null, $cache_life = null)
    {
     $self = & self::getInstance();
        if (!$id) {
            if ($self->_id) {
                $id = $self->_id;
            } else {
                exit('file_cache:save() id 不能为空!');
            }
        }
        $time = time();
       
        if($cache_life) {
         $data[self::FILE_LIFE_KEY] = $time + $cache_life;
        }
  elseif
  ($cache_life != 0){
         $data[self::FILE_LIFE_KEY] = $time + $self->_options['file_life'];
        }
       
        $file = $self->_file($id);
       
        $data = "<?phpn".
                " // mktime: ". $time. "n".
                " return ".
                var_export($data, true).
                "n?>"
                ;
       
        $res = $self->_filePutContents($file, $data);
        return $res;
    }
   
   
    /**
     * 得到缓存信息
     *
     * @param  string  $id  缓存id
     * @return string|array 缓存数据
     */
    static function load($id)
    {
        $self = & self::getInstance();
     $time = time();
     //检测缓存是否存在
     if (!$self->test($id)) {
            // The cache is not hit !
            return false;
        }
       
  //全部清空识别文件
        $clearFile = $self->_file(self::CLEAR_ALL_KEY);
  
  $file = $self->_file($id);
  
        //判断缓存是否已被全部清除
     if(is_file($clearFile) && filemtime($clearFile) > filemtime($file))
     {
      return false;
     }
      
        $data = $self->_fileGetContents($file);
      if(empty($data[self::FILE_LIFE_KEY]) || $time < $data[self::FILE_LIFE_KEY]) {
            unset($data[self::FILE_LIFE_KEY]);
       return $data;   
      }
      return false;
    }   
   
    /**
     * 写入缓存文件
     *
     * @param  string $file   缓存路径
     * @param  string $string 缓存信息
     * @return boolean true 成功
     */
    protected function _filePutContents($file, $string)
    {
     $self = & self::getInstance();
        $result = false;
        $f = @fopen($file, 'ab+');
        if ($f) {
            if ($self->_options['file_locking']) @flock($f, LOCK_EX);
            fseek($f, 0);
            ftruncate($f, 0);
            $tmp = @fwrite($f, $string);
            if (!($tmp === false)) {
                $result = true;
            }
            @fclose($f);
        }
        @chmod($file, $self->_options['cache_file_umask']);
        return $result;
    }
   
    /**
     * 格式化后的缓存文件路径
     *
     * @param  string $id 缓存id
     * @return string 缓存文件名(包括路径)
     */
    protected function _file($id)
    {
     $self = & self::getInstance();
        $fileName = $self->_idToFileName($id);
        return $self->_options['cache_dir'] . $fileName;
    }   
   
    /**
     * 格式化后的缓存文件名字
     *
     * @param  string $id 缓存id
     * @return string 缓存文件名
     */
    protected function _idToFileName($id)
    {
     $self = & self::getInstance();
        $self->_id = $id;
        $prefix = $self->_options['file_name_prefix'];
        $result = $prefix . '---' . $id;
        return $result;
    }  
   
    /**
     * 判断缓存是否存在
     *
     * @param  string $id Cache id
     * @return boolean True 缓存存在 False 缓存不存在
     */
    static function test($id)
    {
     $self = & self::getInstance();
        $file = $self->_file($id);
       
        if (!is_file($file)) {
            return false;
        }
       
        return true;
    }
   
    /**
     * 得到缓存信息
     *
     * @param  string $file 缓存路径
     * @return string 缓存内容
     */
    protected function _fileGetContents($file)
    {
        if (!is_file($file)) {
            return false;
        }
        return include $file;
    }    
   
    /**
     * 清除所有缓存
     *
     * @return void
     */   
    static function clear()
    {
     $self = & self::getInstance();
     $self->save('CLEAR_ALL',self::CLEAR_ALL_KEY); 
    }  
 
    /**
     * 清除一条缓存
     *
     * @param string cache id 
     * @return void
     */  
    static function del($id)
    {
  $self = & self::getInstance();
     if(!$self->test($id)){
      // 该缓存不存在
      return false;
     }
     $file = $self->_file($id);
     return unlink($file);
    } 
}

存入数据

<?php
$config = array(
 'name' => 'xiaojiong',
 'qq'   => '290747680',
 'age'  => '20',
);

//第一个参数 缓存data
//第二个参数 缓存id
//第三个参数 cache_life 0 永不过期(cache::clear()清空所有除外) 默认cache_life 为option_cache_life
cache::save($config,'config',0);

载入数据

<?php
//只有一个参数 cache_id
$config = cache::load('config');

清空缓存

<?php
//清空指定缓存
cache::del('config');
//清空所有缓存
cache::clear();

cache信息配置

//在执行所有cache_func前调用

$_options = array(
    'cache_dir' => './cache', //缓存文件目录
    'file_name_prefix' => 'cache',//缓存文件前缀
    'file_life'  => 100000, //缓存文件生命
);
cache::setOptions($options);

//再执行 就会按着新配置信息执行,否则是默认信息
cache::save($arr,'arr');


//就是这个方法 貌似不合理 望大家指点

统计图形就我们会常到的数据图形了,如果三个数组以图形显示或楼盘以图形走向我们都会要用到图形,下面我来介绍一个php LIbchart图形生成类吧,很用的有需要的朋友可参考。

简单全数字或英文的就可以直接使用下面类了(libchart类大家可自行百度下载)

<?

 代码如下 复制代码

 /*
  update by Leo
  It's draw the pic of Sheet,and it will take all the num on the pic.
 */
 require "./libchart/classes/libchart.php";
 class drawPic{
  var $chart;
  var $style;
  function drawPic($style="1",$width="500",$height="250"){
   $this->style=$style;
   if($style==1){
    //cylinder
    $this->chart = new VerticalBarChart($width,$height);
   }else if($style==2){
    //line
    $this->chart = new LineChart($width,$height);
   }else if($style==3){
    //Lump
    $this->chart = new PieChart($width,$height);
   }else{
    //cross
    $this->chart=new HorizontalBarChart($width,$height);
   }
  }
 
  function draw($obj){
  
   if($this->style==1||$this->style=="1"){
    //cylinder
    $dataSet = new XYDataSet() ;
    $this->chart->setTitle($obj->title);//title
    $arr=array();
    $arr=$obj->dataArray;
    foreach($arr as $key => $val){
     $dataSet->addPoint ( new Point($key,$val)) ;
    }
    $this->chart->setDataSet ( $dataSet ) ;
    $this->chart->render();
   }else if($this->style==2||$this->style=="2"){
    //line
    $this->chart->setTitle($obj->title);//title
    $arr=array();
    $arr=$obj->dataArray;
    $i=0;
    $dataSet = new XYSeriesDataSet();
    foreach($arr as $key => $val){
     $serie{$i}= new XYDataSet();
     foreach($val as $k => $v){
      $serie{$i}->addPoint(new Point($k,$v));
     }
     $dataSet->addSerie($key,$serie{$i});
     $i=$i+1;
    }
    $this->chart->setDataSet($dataSet);
    $this->chart->render();
   }else if($style==3){
    //Lump
    $dataSet = new XYDataSet() ;
    $this->chart->setTitle($obj->title);//title
    $arr=array();
    $arr=$obj->dataArray;
    foreach($arr as $key => $val){
     $dataSet->addPoint ( new Point($key."($val)",$val)) ;
    }
    $this->chart->setDataSet ( $dataSet ) ;
    $this->chart->render();
   }else{
    //cross
    $dataSet = new XYDataSet();
    $this->chart->setTitle($obj->title);//title
    $arr=array();
    $arr=$obj->dataArray;
    foreach($arr as $key => $val){
     $dataSet->addPoint ( new Point($key,$val)) ;
    }
    $this->chart->setDataSet($dataSet);
    $this->chart->render();
   }
  }
 
 }
 class kkk{};
 $n=new drawPic("4");//it will set 1 or 2 or 3 or 4
 $k=new kkk();
 $k->dataArray=array("2000"=>"30","2001"=>"40","2002"=>"50","2003"=>"60","2004"=>"70","2005"=>"80","20020"=>"90");//style==1 or style=2 or style=4
 //$k->dataArray=array("2000"=>"30","2001"=>"40","2002"=>"50","2003"=>"60","2004"=>"70","2005"=>"80","20020"=>"90");//style==3
 //$k->dataArray=array("yi"=>array("2000"=>"30","2001"=>"40","2002"=>"50","2004"=>"60"),"er"=>array("2003"=>"60","2004"=>"70","2005"=>"80","20020"=>"90"),"san"=>array("33"=>"12","45"=>"56","89"=>"45","86"=>"49"));//style==2 and the years will show first array to block.(it will be show 2000 2001 2002 2004)
 $k->title="The Sheet title";
 $n->draw($k);

?>

红色字体为调用。方法1,2,4为相同的数组。3为线性图,有可能有两条线或者多条线的比较(也可以单线)。

如果要使用中文可能会发现libchart中文乱码 了,下面找了一个办法


我们的应用主源代码如下:

 代码如下 复制代码

<?php

   header("content-type:image/png");  

   require_once('libchart/classes/libchart.php');
  
   $chart = new VerticalBarChart( 500 , 250 ) ; // 参数表示需要创建的图像的宽和高

   $dataSet = new XYDataSet() ; // 实例化一个 XY 轴数据对象


     // 为这个对象增加四组数据集合, Point 对象的第一个参数表示 X 轴坐标,
// 第二个表示 Y 轴坐标

   $str = '二月';
   
$str = mb_convert_encoding($str, "html-entities","utf-8" );


$dataSet -> addPoint ( new Point( "Jan 2005" , 273 )) ;

    $dataSet -> addPoint ( new Point( "$str" , 120 )) ;

    $dataSet -> addPoint ( new Point( "March 2005" , 442 )) ;

    $dataSet -> addPoint ( new Point( "April 2005" , 600 )) ;

     // 把这个数据集合传递给图形对象

    $chart -> setDataSet ( $dataSet ) ;

    // 设置图形的标题,并把它作为一个 png 文件渲染

    $chart -> setTitle ( "统计图" ) ;
   
    //$chart -> render ( "demo/generated/demo1.png" ) ;
    // 这里需要一个路径和文件名称
    //就这么简单一个像下图一样美丽的柱状图就出来了

    $chart -> render () ; 

?>

标红字的地方是为了解决中文乱码的。

2、标题乱码:

默认显示中文是乱码,这是由于编码的原因,做如下修改:
首先,更改libchar/libchart/classes/view/chart/Chart.php,找到如下内容:

 代码如下 复制代码
public function setTitle($title) {          
            $this->plot->setTitle($title);
        }

更改为:

 代码如下 复制代码

public function setTitle($title) {
        $title = mb_convert_encoding($title, "html-entities","utf-8" );

           $this->plot->setTitle($title);

}

第三步:就是上面某个博客里讲到的:

1、自己写的使用Libchart 库生成图表的php 文件以utf-8编码保存
       2、找几个中文字体库,比如华文行楷、宋体等等,复制到libchart fonts目录下
        3、修改libchart classes目录下的text.php 文件
第47、48行

 

 代码如下 复制代码

$this->fontCondensed = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed.ttf";
$this->fontCondensedBold = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed-Bold.ttf";

改为


$this->fontCondensed = dirname(__FILE__) . "/../fonts/你找来的中文字体";
$this->fontCondensedBold = dirname(__FILE__) . "/../fonts/你找来的中文字体";


我修改的:

 代码如下 复制代码

public function Text() {
    $baseDir = dirname(__FILE__) . "/../../../";
 
    // Free low-res fonts based on Bitstream Vera <http://dejavu.sourceforge.net/wiki/>
   $this->fontCondensed = $baseDir . "fonts/FANGZHENGFANGSONG.ttf";
    $this->fontCondensedBold = $baseDir . "fonts/FANGZHENGFANGSONG.ttf";
   }

FANGZHENGFANGSONG.ttf 这个文件是我找的方正仿宋简体字库,我把中文名字改成那个样子了,其实不改也是可以的。

主要是基本操作,然后又疑问的地方,按照上面说的做了,应该就是已经可以了,再次谢谢提供这些方法的热心网友,大家多多交流呀!

在php中遍历数据一般会使用到list,foreach,each其实中一种,但下面的教程可能不会用到,下面我来给各位分别介绍一下遍历数组高级实例,希望此方法对大家有帮助。

学习程式语言时, 总是学学 for, 然后再试著用 while 写出 for 的效果 等等的一些练习.

来看看没有 foreach 前, 要想要有 foreach 的功能要怎?写(用 while、list、each 来达成).


在这篇文章看到: PHP的foreach前身写法 

 代码如下 复制代码

//旧的写法
reset($attributes);
while (list($key, $value) = each($attributes)) {
    //do something
}

//PHP4版本新增
foreach ($attributes as $key => $value){
   //do something
}

多维关联数组排序
PHP提供了一些数组排序的函数,比如sort(), ksort(),和asort(),但是却没有提供对多维关联数组的排序。


比如这样的数组:

 代码如下 复制代码

Array
(
  [0] => Array
    (
      [name] => chess
      [price] => 12.99
    )

  [1] => Array
    (
      [name] => checkers
      [price] => 9.99
    )

  [2] => Array
    (
      [name] => backgammon
      [price] => 29.99
    )
)

要将该数组按照升序排序,你需要自己写一个函数用于比较价格,然后将该函数作为回调函数传递给usort()函数来实现该功能:

 代码如下 复制代码

function comparePrice($priceA, $priceB){
    return $priceA['price'] - $priceB['price'];
}

usort($games, 'comparePrice');执行了该程序片段,数组就会被排序,结果如下所示:

Array
(
  [0] => Array
    (
      [name] => checkers
      [price] => 9.99
    )

  [1] => Array
    (
      [name] => chess
      [price] => 12.99
    )

  [2] => Array
    (
      [name] => backgammon
      [price] => 29.99
    )
)

要将该数组按照降序排序,把comparePrice()函数里面的两个减的数调换位置就可以了。

逆序遍历数组
PHP的While循环和For循环是遍历一个数组最常用的方法。但是你怎样遍历像下面这个数组呢?

 代码如下 复制代码

Array
(
  [0] => Array
    (
      [name] => Board
      [games] => Array
        (
          [0] => Array
            (
              [name] => chess
              [price] => 12.99
            )

          [1] => Array
            (
              [name] => checkers
              [price] => 9.99
            )
        )
    )
)

PHP标准库中有一个对集合的迭代器iterators类,它不仅仅能够用于遍历一些异构的数据结构(比如文件系统和数据库查询结果集),也可以对一些不知道大小的嵌套数组的遍历。比如对上面的数组的遍历,可以使用RecursiveArrayIterator迭代器进行:

 代码如下 复制代码

$iterator = new RecursiveArrayIterator($games);
iterator_apply($iterator, 'navigateArray', array($iterator));

function navigateArray($iterator) {
 while ($iterator->valid()) {
  if ($iterator->hasChildren()) {
   navigateArray($iterator->getChildren());
  } else {
   printf("%s: %s", $iterator->key(), $iterator->current());
  }
  $iterator->next();
 } 
}

执行该段代码会给出以下的结果:

name: Board
name: chess
price: 12.99
name: checkers
price: 9.99

过滤关联数组的结果
假定你得到了如下一个数组,但是你仅仅想操作价格低于$11.99的元素:

 代码如下 复制代码

Array
(
  [0] => Array
    (
      [name] => checkers
      [price] => 9.99
    )

  [1] => Array
    (
      [name] => chess
      [price] => 12.99
    )

  [2] => Array
    (
      [name] => backgammon
      [price] => 29.99
    )
)

使用array_reduce()函数可以很简单的实现:

 代码如下 复制代码

function filterGames($game){
 return ($game['price'] < 11.99);
}

$names = array_filter($games, 'filterGames');

array_reduce()函数会过滤掉不满足回调函数的所有的元素,本例子的回调函数就是filterGames。任何价格低于11.99的元素会被留下,其他的会被剔除。该代码段的执行结果:

 代码如下 复制代码
Array
(
  [1] => Array
    (
      [name] => checkers
      [price] => 9.99
    )
)

对象转换成数组
一个需求就是将对象转换成数组形式,方法比你想象的简单很多,仅仅强制转换就可以了!例子:

 代码如下 复制代码

class Game {
 public $name;
 public $price;
}

$game = new Game();
$game->name = 'chess';
$game->price = 12.99;

print_r(array($game));执行该例子就会产生如下结果:

Array
(
[0] => Game Object
  (
    [name] => chess
    [price] => 12.99
  )
)

将对象转换成数组会出现一些不可预料的副作用。比如上面的代码段,所有的成员变量都是public类型的,但是对于private私有变量的返回结果会变得不一样。下面是另外一个例子:

 代码如下 复制代码

class Game {
 public $name;
 private $_price;

 public function setPrice($price)  {
  $this->_price = $price;
 }
}

$game = new Game();
$game->name = 'chess';
$game->setPrice(12.99);

print_r(array($game));执行该代码片段:

Array
(
[0] => Game Object
  (
    [name] => chess
    [_price:Game:private] => 12.99
  )
)

正如你所看到的,为了进行区分,数组中保存的私有变量的key被自动改变了。

数组的“自然排序”
PHP对于“字母数字”字符串的排序结果是不确定的。举个例子,假定你有很多图片名称存放于数组中:

 代码如下 复制代码
$arr = array(
 0=>'madden2011.png',
 1=>'madden2011-1.png',
 2=>'madden2011-2.png',
 3=>'madden2012.png'
);

你怎样对这个数组进行排序呢?如果你用sort()对该数组排序,结果是这样的:

 代码如下 复制代码
Array
(
    [0] => madden2011-1.png
    [1] => madden2011-2.png
    [2] => madden2011.png
    [3] => madden2012.png
)

有时候这就是我们想要的,但是我们想保留原来的下标怎么办?解决该问题可以使用natsort()函数,该函数用一种自然的方法对数组排序:

 代码如下 复制代码

<?php
$arr = array(
 0=>'madden2011.png',
 1=>'madden2011-1.png',
 2=>'madden2011-2.png',
 3=>'madden2012.png'
);

natsort($arr);
echo "<pre>"; print_r($arr); echo "</pre>";
?>

运行结果:

Array
(
    [1] => madden2011-1.png
    [2] => madden2011-2.png
    [0] => madden2011.png
    [3] => madden2012.png
)

本文章来给大家总结几个不错的php排序算法,希望这些算法对各位同学会有所帮助哦。
 代码如下 复制代码


1.冒泡算法,排序算法,由于在排序过程中总是小数往前放,大数往后放,相当于气泡往上升,所以称作冒泡排序

$array = array(a,f,c,b,e,h,j,i,g); 

   function maopao_fun($array){ 

       if($len <= 1) { 

           return $arr; 

       } 

       $count = count($array); 

       for($i=0;$i<$count;$i++){ 

           for($j=$count-1;$j>$i;$j--){ 

               if($array[$j] > $array[$j-1]){ 

                   $tmp = $array[$j]; 

                   $array[$j] = $array[$j-1]; 

                   $array[$j-1] = $tmp; 

               } 

           } 

       } 

       return $array; 

   } 

2.快速排序,
快速排序(Quicksort)是对冒泡排序的一种改进。
由C. A. R. Hoare在1962年提出。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,
其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,
整个排序过程可以递归进行,以此达到整个数据变成有序序列。

function quickSort($arr){ 

       $len = count($arr); 

       if($len <= 1) { 

           return $arr; 

       } 

       $key = $arr[0]; 

       $left_arr    = array(); 

       $right_arr    = array(); 

       for($i=1; $i<$len; $i++){ 

           if($arr[$i] <= $key){ 

               $left_arr[] = $arr[$i]; 

           } else { 

               $right_arr[] = $arr[$i]; 

           } 

       } 

       $left_arr    = quickSort($left_arr); 

       $right_arr    = quickSort($right_arr); 

       return array_merge($left_arr, array($key), $right_arr); 

   } 

3.选择排序
每一趟从待排序的数据元素中选出最小(或最大)的一个元素,
顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。 选择排序是不稳定的排序方法

 代码如下 复制代码

function select_sort($arr){ 

    $count = count($arr); 

    for($i=0; $i<$count; $i++){ 

        for($j=$i+1; $j<$count; $j++){ 

            if ($arr[$i] > $arr[$j]){ 

                $tmp = $arr[$i]; 

                $arr[$i] = $arr[$j]; 

                $arr[$j] = $tmp; 

            } 

        } 

    } 

    return $arr; 

4.插入排序
  从第一个元素开始,该元素可以认为已经被排序
  取出下一个元素,在已经排序的元素序列中从后向前扫描
  如果该元素(已排序)大于新元素,将该元素移到下一位置
  重复步骤3,直到找到已排序的元素小于或者等于新元素的位置
  将新元素插入到下一位置中
  重复步骤2

 代码如下 复制代码

function insert_sort($arr){ 

        $count = count($arr); 

        for($i=1; $i<$count; $i++){ 

            $tmp = $arr[$i]; 

            $j = $i - 1; 

            while($arr[$j] > $tmp){ 

                $arr[$j+1] = $arr[$j]; 

                $arr[$j] = $tmp; 

                $j--; 

            } 

        } 

        return $arr; 

    } 

  

    $arr = array(49,38,65,97,76,13,27); 

    print_r(insert_sort($arr));


 

四种排序算法的PHP实现

1) 插入排序(Insertion Sort)的基本思想是:
每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子文件中的适当位置,直到全部记录插入完成为止。
2) 选择排序(Selection Sort)的基本思想是:
每一趟从待排序的记录中选出关键字最小的记录,顺序放在已排好序的子文件的最后,直到全部记录排序完毕。
3) 冒泡排序的基本思想是:
两两比较待排序记录的关键字,发现两个记录的次序相反时即进行交换,直到没有反序的记录为止。
4) 快速排序实质上和冒泡排序一样,都是属于交换排序的一种应用。所以基本思想和上面的冒泡排序是一样的。

 代码如下 复制代码

<?php
/**
 * 四种排序算法设计(PHP)
 *
 * 1) 插入排序(Insertion Sort)的基本思想是:
   每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子文件中的适当位置,直到全部记录插入完成为止。
   2) 选择排序(Selection Sort)的基本思想是:
      每一趟从待排序的记录中选出关键字最小的记录,顺序放在已排好序的子文件的最后,直到全部记录排序完毕。
   3) 冒泡排序的基本思想是:
      两两比较待排序记录的关键字,发现两个记录的次序相反时即进行交换,直到没有反序的记录为止。
   4) 快速排序实质上和冒泡排序一样,都是属于交换排序的一种应用。所以基本思想和上面的冒泡排序是一样的。
 *
 * @author quanshuidingdang
 */
class Sort {
 private $arr  = array(); 
 private $sort = 'insert';
 private $marker = '_sort';
 
 private $debug = TRUE;
 
 /**
  * 构造函数
  *
  * @param array 例如: $config = array (
         'arr' => array(22,3,41,18) ,  //需要排序的数组值
         'sort' => 'insert',      //可能值: insert, select, bubble, quick
         'debug' => TRUE        //可能值: TRUE, FALSE
         )
  */
 public function __construct($config = array()) {
  if ( count($config) > 0) {
   $this->_init($config);
  }
 }
 
 /**
  * 获取排序结果
  */
 public function display() {
  return $this->arr;
 }
 
 /**
  * 初始化
  *
  * @param array
  * @return  bool
  */
 private function _init($config = array()) {
  //参数判断
  if ( !is_array($config) OR count($config) == 0) {
   if ($this->debug === TRUE) {
    $this->_log("sort_init_param_invaild");
   }
   return FALSE;
  }
  
  //初始化成员变量
  foreach ($config as $key => $val) {
   if ( isset($this->$key)) {
    $this->$key = $val;
   }
  }
  
  //调用相应的成员方法完成排序
  $method = $this->sort . $this->marker;
  if ( ! method_exists($this, $method)) {
   if ($this->debug === TRUE) {
    $this->_log("sort_method_invaild");
   }
   return FALSE;
  }
  
  if ( FALSE === ($this->arr = $this->$method($this->arr)))
   return FALSE;
  return TRUE;
 }
 
 /**
  * 插入排序
  *
  * @param array
  * @return bool
  */
 private function insert_sort($arr) {
  //参数判断
  if ( ! is_array($arr) OR count($arr) == 0) {
   if ($this->debug === TRUE) {
    $this->_log("sort_array(insert)_invaild");
   }
   return FALSE;
  }
  
  //具体实现
  $count = count($arr);
  for ($i = 1; $i < $count; $i++) {
   $tmp = $arr[$i];
   for($j = $i-1; $j >= 0; $j--) { 
    if($arr[$j] > $tmp) {
     $arr[$j+1] = $arr[$j];
     $arr[$j] = $tmp;
    }
   }
  }
  return $arr;
 }
 
 /**
  * 选择排序
  *
  * @param array
  * @return bool
  */
 private function select_sort($arr) {
  //参数判断
  if ( ! is_array($arr) OR count($arr) == 0) {
   if ($this->debug === TRUE) {
    $this->_log("sort_array(select)_invaild");
   }
   return FALSE;
  }
  
  //具体实现
  $count = count($arr);
  for ($i = 0; $i < $count-1; $i++) {
   $min = $i;
   for ($j = $i+1; $j < $count; $j++) {
    if ($arr[$min] > $arr[$j])  $min = $j;
   }
   if ($min != $i) {
    $tmp = $arr[$min];
    $arr[$min] = $arr[$i];
    $arr[$i] = $tmp;
   }
  }
  return $arr;
 }
 
 /**
  * 冒泡排序
  *
  * @param array
  * @return bool
  */
 private function bubble_sort($arr) {
  //参数判断
  if ( ! is_array($arr) OR count($arr) == 0) {
   if ($this->debug === TRUE) {
    $this->_log("sort_array(bubble)_invaild");
   }
   return FALSE;
  }
  
  //具体实现
  $count = count($arr);
  for ($i = 0; $i < $count; $i++) {
   for ($j = $count-1; $j > $i; $j--) {
    if ($arr[$j] < $arr[$j-1]) {
     $tmp = $arr[$j];
     $arr[$j] = $arr[$j-1];
     $arr[$j-1] = $tmp;
    }
   }
  }
  return $arr; 
 }
 
 /**
  * 快速排序
  *
  * @param array
  * @return bool
  */
 private function quick_sort($arr) {
  //具体实现
  if (count($arr) <= 1) return $arr;
  $key = $arr[0];
  $left_arr = array();
  $right_arr = array();
  for ($i = 1; $i < count($arr); $i++){
   if ($arr[$i] <= $key)
    $left_arr[] = $arr[$i];
   else
    $right_arr[] = $arr[$i];
  }
  $left_arr = $this->quick_sort($left_arr);
  $right_arr = $this->quick_sort($right_arr);

  return array_merge($left_arr, array($key), $right_arr);
 }
 
 /**
  * 日志记录
  */
 private function _log($msg) {
  $msg = 'date[' . date('Y-m-d H:i:s') . '] ' . $msg . 'n';
  return @file_put_contents('sort_err.log', $msg, FILE_APPEND);
 }
}

/*End of file sort.php*/
/*Location htdocs/sort.php */

[!--infotagslink--]

相关文章

  • php二维码生成

    本文介绍两种使用 php 生成二维码的方法。 (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param strin...2015-10-21
  • Java生成随机姓名、性别和年龄的实现示例

    这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
  • C#生成随机数功能示例

    这篇文章主要介绍了C#生成随机数功能,涉及C#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • php生成唯一数字id的方法汇总

    关于生成唯一数字ID的问题,是不是需要使用rand生成一个随机数,然后去数据库查询是否有这个数呢?感觉这样的话有点费时间,有没有其他方法呢?当然不是,其实有两种方法可以解决。 1. 如果你只用php而不用数据库的话,那时间戳+随...2015-11-24
  • jQuery为动态生成的select元素添加事件的方法

    下面小编就为大家带来一篇jQuery为动态生成的select元素添加事件的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-09-01
  • PHP自动生成后台导航网址的最佳方法

    经常制作开发不同的网站的后台,写过很多种不同的后台导航写法。 最终积累了这种最写法,算是最好的吧...2013-09-29
  • js生成随机数的方法实例

    js生成随机数主要用到了内置的Math对象的random()方法。用法如:Math.random()。它返回的是一个 0 ~ 1 之间的随机数。有了这么一个方法,那生成任意随机数就好理解了。比如实际中我们可能会有如下的需要: (1)生成一个 0 - 1...2015-10-21
  • PHP验证码生成与验证例子

    验证码是一个现在WEB2.0中常见的一个功能了,像注册、登录又或者是留言页面,都需要注册码来验证当前操作者的合法性,我们会看到有些网站没有验证码,但那是更高级的验证了,...2016-11-25
  • PHP生成不同颜色、不同大小的tag标签函数

    复制代码 代码如下:function getTagStyle(){ $minFontSize=8; //最小字体大小,可根据需要自行更改 $maxFontSize=18; //最大字体大小,可根据需要自行更改 return 'font-size:'.($minFontSize+lcg_value()*(abs($maxFo...2013-10-04
  • JS生成某个范围的随机数【四种情况详解】

    下面小编就为大家带来一篇JS生成某个范围的随机数【四种情况详解】。小编觉得挺不错的,现在分享给大家,也给大家做个参考,一起跟随小编过来看看吧...2016-04-22
  • php中利用str_pad函数生成数字递增形式的产品编号

    解决办法:$str=”QB”.str_pad(($maxid[0]["max(id)"]+1),5,”0″,STR_PAD_LEFT ); 其中$maxid[0]["max(id)"]+1) 是利用max函数从数据库中找也ID最大的一个值, ID为主键,不会重复。 str_pad() 函数把字符串填充为指...2013-10-04
  • C#生成Word文档代码示例

    这篇文章主要介绍了C#生成Word文档代码示例,本文直接给出代码实例,需要的朋友可以参考下...2020-06-25
  • Vue组件文档生成工具库的方法

    本文主要介绍了Vue组件文档生成工具库的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-11
  • PHP简单实现生成txt文件到指定目录的方法

    这篇文章主要介绍了PHP简单实现生成txt文件到指定目录的方法,简单对比分析了PHP中fwrite及file_put_contents等函数的使用方法,需要的朋友可以参考下...2016-04-28
  • 史上最简洁C# 生成条形码图片思路及示例分享

    这篇文章主要介绍了史上最简洁C# 生成条形码图片思路及示例分享,需要的朋友可以参考下...2020-06-25
  • 简单入门级php 生成xml文档代码

    $doc = new domdocument('1.0'); // we want a nice output $doc->formatoutput = true; 代码如下 复制代码 $root = $doc->createelement('bo...2016-11-25
  • 解决PHPWord导出生成Word中文乱码问题

    PHPWord是一个可以把相关文件生成word文档的插件了,但由于是老外开发的对于gbk支持不好,所以我们在导出带有中文的word时会出现乱码问题,下面本文就PHPWord中文乱码问题...2016-11-25
  • php 验证码详细生成与使用方法

    注意:以下代码需要打开php教程的gd库,修改php.in文件的配置,把已经注释掉的行之前的分号取消即可:extension=php_gd2.dll。 $width = 165; $height = 120; ...2016-11-25
  • 用Java生成二维码并附带文字信息

    这篇文章主要介绍了用Java生成二维码并附带文字信息,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下...2021-04-29
  • php生成二维码程序代码

    生成二维码程序在网上可以找到很多我们发现像google,还有一些国外站都提供了免费生成二维码的插件或api了,下面我来给大家介绍几种。 最简单最实例的goolge开源方法...2016-11-25