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

PHP sendmail

最近很多人问我如何发邮件,为什么mail函数无法发出去。我当时感慨了一下下。。。
mail函数是linux下面默认的发邮件函数,而且需要安装mail服务,同时要在PHP.ini里面配置好才能发送。 也有人嫌phpmailer太大,正好手头有一个discuz发邮件的类改的函数。贴上来。。。抛抛砖引引玉,不要砸人就行。
毛主席说:要温柔。。。

» 阅读全文

Tags: discuz, sendmail, 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

利用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

PHP连贯接口

  近期,看到很多人都在讨论连贯接口这个东西,仔细想想,这其实没有什么,用的早的如javascript中的jQuery,都早就在用了。只是PHP最近被人翻出来炒一炒而已。

  但不得不说的是,连贯接口也有它自身的好处,即可以让代码更清晰化,比如Zend的Select类里面,生成一个SQL并执行就是那样的直观:

PHP代码
  1. <?php  
  2. $db = new Zend_Db(); //这是随便写的,主要是为了显示一下  
  3. $db->select()  
  4.    ->from('tablename')  
  5.    ->where('id = ?' , $id)  
  6.    ->orwhere('name != ?' , $name);  
  7.   
  8. //最后也可以调用一下来生成这个SQL  
  9. $sql = $db->__toString();  

自从到了PHP5,$a = new Object() ; $b = $a; 这个$b 直接变成了取址,而不是COPY后,这样的连贯接口比之用数组来实现效率是高上了很多,毕竟相对占用内存会少一点吧?(估计,没有实测过)。

 

不过,这东西确实也就和AJAX一样,属于老瓶装新酒。以前类里的函数是返回一个值,或者true等等等等,现在是return $this;直接返回自身(不知道这样效率是提升还是下降。),但总体来说,这个还是一个很不错的方法。

反正,自从我在Zend的FW里看到这个连贯接口后,接下来,在很多地方就看到了不同的介绍、应用。慢慢享受吧。HOHO

Tags: php