手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表分类:PHP

PHP中Eval的作用

eval是什么,相信很多都会知道。但真实有多少人使用它呢?恐怕在实际应用中,使用的也比较少吧。(详细请看全文)

Evaluates the string given in code_str as PHP code. Among other things, this can be useful for storing code in a database text field for later execution.

There are some factors to keep in mind when using eval(). Remember that the string passed must be valid PHP code, including things like terminating statements with a semicolon so the parser doesn't die on the line after the eval(), and properly escaping things in code_str. To mix HTML output and PHP code you can use a closing PHP tag to leave PHP mode.

Also remember that variables given values under eval() will retain these values in the main script afterwards.

» 阅读全文

Tags: php

PHP一些常用的计算时间方法

 

在PHP里面,要想统计一个action或者一个函数或者某个过程的执行所消耗的时间往往都只有一个办法:在运算前记录下时间戳,在运算后记录下时间戳,然后相减,就能得到一个相对比较实际的时间。

基本代码如下(从phpmyadmin里复制而来,懒得打了。这段代码其实也就是phpMyadmin里SQL的执行时间的计算):

PHP代码
  1. <?php   
  2.     // garvin: Measure query time.   
  3.     // TODO-Item http://sourceforge.net/tracker/index.php?func=detail&aid=571934&group_id=23067&atid=377411   
  4.     $querytime_before = array_sum(explode(' ', microtime()));   
  5.   
  6.     $result   = @PMA_DBI_try_query($full_sql_query, null, PMA_DBI_QUERY_STORE);   
  7.   
  8.     $querytime_after = array_sum(explode(' ', microtime()));   
  9.   
  10.     $GLOBALS['querytime'] = $querytime_after - $querytime_before;   
  11.   
  12. ?>  

 其中 array_sum(explode(' ', microtime())); 是PHP4时代的写法,到PHP5之后,microtime函数多了一个bool值的参数,加上这个参数后可以直接得到 array_sum(explode(' ', microtime())); 相等的值,即:microtime(true);

本文并无技术含量,纯粹用来记录一下。

Tags: 计算时间

JpGraph中文乱码完美解决方案[转]

原文地址:http://lizi.blogbus.com/logs/12601515.html

 

XML/HTML代码
  1. 很多人使用JpGraph会出现乱码问题,但是网上有好多并无效果的解决方式,经过反复试验及在网上看了其它人的经验,总结出在2.3版下最终较完美解决方案   
  2. 1、把simsun.ttc字体拷到服务器/usr/X11R6/lib/X11/fonts/truetype下,当然这个目录可以自定义   
  3. 2、不需要更改$aFF === FF_SIMSUN或$aFF === FF_CHINESE ,直接用以下代码即可   
  4. $title = "JpGraph中文测试";   
  5. $title = iconv("UTF-8", "gb2312", $title);   
  6. $graph->title->Set($title);   
  7. 就可以完美的解决中文问题了   
  8. 3、还有一个问题,就是右上方的中文显示问题,就是SetLegend函数,解决方式如下   
  9. 打开jpgraph.php文件,找到   
  10. private $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12;   
  11. 用   
  12. private $font_family=FF_SIMSUN,$font_style=FS_NORMAL,$font_size=8;   
  13. 替换就OK了^_^  

后记:本来都忘了这个了,正好有朋友问起类似的问题,于是又GOOGLE了一下,找到了这个,记录一下,也为GOOGLE的收录作点贡献,这样以后再有人遇到类似问题,更容易被搜索到。

利用array_diff函数更新tag

先介绍一下array_diff函数

JavaScript代码
  1. array_diff -- 计算数组的差集   
  2. 说明   
  3. array array_diff ( array array1, array array2 [, array ...] )   
  4.   
  5.   
  6. array_diff() 返回一个数组,该数组包括了所有在 array1 中但是不在任何其它参数数组中的值。注意键名保留不变。    
  7.   
  8. 注意: 两个单元仅在 (string) $elem1 === (string) $elem2 时被认为是相同的。也就是说,当字符串的表达是一样的时候。    
  9.   
  10. 注意: 注意本函数只检查了多维数组中的一维。当然可以用 array_diff($array1[0], $array2[0]); 检查更深的维度。    
  11.   
  12. ::::::本函数在 PHP 4.0.4 中是坏的! :::::   

在网站的实际应用中,我们可能会用到TAG(标签),对标签的更新可能也会经常遇到,当然添加了之后不修改另作别论。

比如,我们为一条新闻添加了如下标签:新闻,CGX,艳照门 (开个玩笑,呵呵),存入数据库后,应该是在tag表里存入类似于这样的数据 

XML/HTML代码
  1. id      newsid      tagname   
  2. 1       1       新闻   
  3. 2       1       CGX   
  4. 3       1       艳照门  

其中id是自增字段,newsid是新闻的ID,tagname是刚刚存入的标签(存的时候以逗号分隔)。

然而现实总是残酷的,XXX总局规定我们不得把“艳照门”作为关键字了,于是,我们不得不更新TAG(当然,直接从数据库里delete from table where tagname='艳照门'更方便,但这里我是在举例),于是新的TAG就可能变成了:新闻,CGX,其他图片。更新的时候,我们要从数据库里把“艳照门”去除掉,但怎么知道我把原来的标签“艳照门”删除了呢?这时候,array_diff就有用了。

PHP代码
  1. <?php   
  2. //隐藏字段:原始标签   
  3. $_POST['origin_tags'] = '新闻,CGX,艳照门';   
  4. //标签字段   
  5. $_POST['news_tags']   = '新闻,CGX,其他图片';   
  6.   
  7. $origin_tags = explode',', trim( $_POST['origin_tags'] ) );   
  8. $new_tags    = explode',', trim( $_POST['news_tags'] ) );   
  9.   
  10. $diff_tags  = array_diff$origin_tags , $new_tags );   
  11.   
  12. echo "<pre>";   
  13. print_r ( $diff_tags ) ;   
  14. echo "</pre>";   
  15.   
  16. ?>  

看,我们把“艳照门”打印出来了,于是,下一步我们就可以执行:DELETE FROM tablename WHERE news_id = 2 AND tagname = '艳照门';来删除这条记录,同时将新的数据插入数据库(新的数据判断重复可以采用array_intersect,使用方法与array_diff类似,这里就不提了,当然更方便的方法是把:$diff_tags = array_diff( $origin_tags , $new_tags ); 改为 $diff_tags = array_diff( $new_tags , $origin_tags );)

更新数据的方法有很多种,我在这里只是提出一个解决方案。希望大家能够给出其他更好的建议。

Tags: php, tag

Easy way to find a file in subdirectory with php

原文由双瞳翻译。(这里是简要,详情请看全文)

发布时间:08-02-18
原文:
Antoine Ughetto has posted an easy way to find a file inside any series of subdirectories with PHP:
I've made a previous post on SPL using RecursiveIterator to parse Array. Today we will use this same RecursiveIterator with a DirectoryRecursiveIterator.
The code is pretty simple and straight forward - a DirectorySearch class that takes in the directory name and recurses through the files and subdirectories to locate the file you're looking for.
译文:
Antoine Ughetto有个简便的方法,用PHP 来查找一堆子文件夹中的文件。
我先前在 SPL 上发过一篇关于用 RecursiveIterator 追踪数组的文章。现在我们将再次使用RecursiveIterator连同DirectoryRecursiveIterator。
代码相当简单明了-一个DirectorySearch类提取文件目录名称并递归所有文件和子目录, 定位查找文件。

» 阅读全文

Tags: php, spl