帖子标记 ‘PHP’

I-nfotech 有一篇文章列出十個每一個 PHP 開發者都應該認識的開發項目, 我們都知道 擁有全世界最龐大的原碼程式庫(不知道的話現在仍不算遲),所以在開發過程中幾乎所有你需要的功能都已經有人寫好了,你只需插入你的項目中便可以使用,文章的作者有多年的網頁開發經驗,他總結多年的心得,列出了十個每一個 開發者都應該認識的 函式庫。你的要求可能不同,但這篇文章的內容仍然很值得參考。
(全文 …)

PclZip是一个强大的压缩与解压缩zip文件的PHP类,PclZip library不仅能够压缩与解压缩Zip格式的文件;还能出压缩档的内容。同时也可以对现有的ZIP包进行添加或删除文件。

官方网站:http://www.phpconcept.net/pclzip/index.php

以下为一个简单的压缩全站进行备份的代码:
(全文 …)

对于一些转载性质或采集性质的网站,因为外链图片有可能被来源网站删除或出现防盗链的情况,有的时候站长期望把文章中的外联图片都保存到本地空间里。解决办法是在原有的系统中添加一个PHP自动保存文章中外部图片的功能。

首先我们想到的是可以通过正则匹配来寻找文章中所有的img标签,这个表达式需要可以匹配跨行的img标签,并且需要对img标签做条件判断允许img标签带有其他属性.解决方案是使用preg_replace_callback() 这个函数。

function getRomatePic($data){
        $pattern = '/<img[^\/]*src=\"([^\"]*)\"[^\/]*\/>/ims';
        return preg_replace_callback($pattern,filter_image_call, $data);    
}

为了完成外部图片的链接过滤及图片的本地保存,定义filter_image_call()。

function filter_image_call($match){
                $postImagePath="pic/";
                $postImageUrlBase="http://localhost/pic/";
        $image_tag = $match[0]; //获得匹配的img标签
        $image_url = $match[1]; //匹配img标签的src属性值
        //如果src属性值不是http://开头的,也就是说图片已经是本地地址,不做任何修改而返回原始的img标签
        if(substr($image_url, 0, 7) != 'http://'){
                return $image_tag;
        }
        $postfix = date('Y-m-d');
        $dir_prefix = $postImagePath.$postfix."/"; //预定义的本地保存图片的文件夹,根据需要改变
        $url_prefix = $postImageUrlBase.$postfix."/"; //预定义的url前缀,根据需要改变
        //echo $url_prefix;
        //新建保存图片的文件夹
        if(!file_exists($dir_prefix)){
                mkdir($dir_prefix, 0777, true);
        }
        //随机生成图片文件名
        $arr = split("[/\\.]", $image_url);
        $ext =  '.'.$arr[count($arr) - 1];
        $file_name = substr(sha1(date('Y-m-d H:i:s') . rand(0,1000)), 0, 5) .rand_str(5) . $ext;
        //使用http_get_file函数得到远程图片文件,并保存到本地
        $file = http_get_file($image_url);
  file_put_contents($dir_prefix . $file_name, $file);
        //通过str_replace函数替换掉原始img标签中的src属性/
        return str_replace($image_url, $url_prefix . $file_name, $image_tag);
}

最后,定义获取图片的函数,也可以使用discuz中的dfopen函数

function http_get_file($url){
        $url_stuff = parse_url($url);
        $port = isset($url_stuff['port']) ? $url_stuff['port']:80;
        $fp = fsockopen($url_stuff['host'], $port);
        $query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n";
        $query .= 'Host: ' . $url_stuff['host'];
        $query .= "\n\n";
        fwrite($fp, $query);
        while ($line = fread($fp, 1024)) {
                $buffer .= $line;
        }
        preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts);
        return substr($buffer, - $parts[1]);
}

另外中间会用到的还有生成指点数目的随机字符串:

function rand_str($size=6,$feed="abcdefghijklmnopqrstuvwxyz0123456789"){
                for ($i=0; $i < $size; $i++) {
                                $rand_str .= substr($feed, rand() % strlen($feed), 1);
                }
           return $rand_str;
        }

整个方法如上,高手可以将上述修改为 WordPress插件,Discuz插件,PHPWind插件等。

1.基础知识

1.1 什么是Shell编程?

在 Unix 中,shell 可不是简单的命令解释器(典型的有 Windows 中的 DOS ),而是一个全功能的编程环境。Shell 是操作系统的一部分,用来与用户打交道,并且可以用来协调各个命令【1】。用Shell编程可以灵活地解决大量重复任务,十分方便。但是,Shell的语 法十分怪异(个人意见),不容易记,如果现在熟悉的语言可以用来写shell那就好了——比如php——就可以快速开发Shell程序了(比如我的Preminder的后台程序),于是便有了这篇文章,本文以Linux为例说明php-cli的用法,其它平台的版本类似。

1.2 什么是php-cli?

刚才说到,我们可以用php来开发Shell程序。有的同学可能会问啦:“php不是用来做网页的么?-_-”。是的,php可以用来做动态网页,并且当初php就是为做动态网页而开发的语言,但是理论上php可以用来做任何的程序,甚至是桌面程序,而-cli是php在命令行运行的支持环境,也就是我们说的可以用来写Shell的环境支持。

php-cli是php Command Line Interface的简称,如同它名字的意思,就是php在命令行运行的接口,区别于在Web服务器上运行的php环境(php-cgi, isapi等)【2】。

也就是说,php不单可以写前台网页,它还可以用来写后台的程序。
(全文 …)

PHP里面,有众多的函数检查一个变量是否存在,或者是否为真,或者是否为空。例如: empty, is_null, isSet, == null等等,你知道他们之间的细节差异吗?表中的T就是True, F就是False

现在,PHP文档组在其 官方PHP手册中也已经添加了该规则 Appendix K. PHP type comparison tables

检查变量的函数

gettype() empty() is_null() isSet() (bool)
$x = “”; string T F T F
$x = null; NULL T T F F
var $x; (not set) NULL T T F F
$x = array(); array T F T F
$x = false; boolean T F T F
$x = 15; integer F F T T
$x = 1; integer F F T T
$x = 0; integer T F T F
$x = -1; integer F F T T
$x = “15″; string F F T T
$x = “1″; string F F T T
$x = “0″; string T F T F
$x = “-1″; string F F T T
$x = “foo”; string F F T T
$x = “true”; string F F T T
$x = “false”; string F F T T

(全文 …)

在Web开发/网站设计世界里,PHP是最流行的语言之一,从PHP里,你能够很容易的找到你所需的脚本,遗憾的是,很少人会去用“最佳做法”去写一个 PHP程序。这里,我们向大家介绍PHP的10种最佳实践,当然,每一种都是经过大师们证明而得出的。

1. 在合适的时候使用PHP – Rasmus Lerdorf

没有谁比PHP的创建者Rasmus Lerdorf明白PHP用在什么地方是更合理的,他于1995年发布了PHP这门语言,从那时起,PHP就像燎原之火,烧遍了整个开发阵营,改变了互联 网的世界。可是,Rasmus并不是因此而创建PHP的。PHP是为了解决web开发者的实际问题而诞生的。

和许多开源项目一样,PHP变得流行,流行的动机并不能用正常的哲学来进行解释,甚至流行得有些 孤芳自赏。它完全可以作为一个案例,一个解决各种Web问题的工具需求所引起的案例,因此当PHP刚出现的时候,这种工具需求全部聚焦到PHP的身上。
(全文 …)

使用PHP自动生成密码

In this tutorial I will show you how to generate random passwords that are highly secure and extremely difficult to crack. However you can choose between various complexity/strength and you can set password length as well.

Step 1.

Let’s go through what we need to generate passwords. First we need a list of words and/or characters what we can use for password generation. I don’t offer using word lists as it is easier to guess and password recovery tools are using such lists as well. So I will focus only on character lists.

The idea is to create a string from the characters and than in a loop we select an item from this string (character list) one by one until we reach the requested length. To realize this we will implement a function with 2 parameters. The first is the length of the requested password and the second is the strength/complexity of the password.
(全文 …)

PHP的可变变量名

作者: seasun

有时候可变的变量名会给编程带来很大的方便。也就是说变量名可以被动态的命名和使用。通常变量通过下面这样的语句来命名 :

<?
$a = 'hello';
?>

可变变量名指的是使用一个变量的值作为这个变量的名称。在上面的例子中,通过使用两个$符号,你可以把hello设置成一个变量 的名称,就像下面那样。

<?php
$$a = 'world';
?>

(全文 …)

Drupal是一个内容管理系统(CMS),也是一个内容管理框架(CMF)。因此,它既是一种应用程序,能够帮助开发者外的其它人员建立复杂的网 站;同时,它也是一种开发架构,能够帮助开发人员快速定制和开发应用程序。围绕Drupal,已经发展起来一个庞大的用户社区,热衷于改进该软件,并支持它的实际应用。

经过两年多的酝酿和6600多次重大更新, 7 即将发布。其中的重大变化会对最终用户,网站建设者和开发者带来很好的影响。本文探讨了Drupal 7 一些最显著的新功能。首先,让我们介绍一些入门者必须了解的Drupal基本知识…

Drupal的基础知识

在 Drupal中有五个层次:
1。 数据 – 在Drupal中,表现为’节点’(文章,评论,用户)。
2。 模块 – 即Drupal中的一些功能插件,用来扩展你的网站的功能。
3。 区块,菜单 – 提供一个网页区域,其中包括导航。
4。 用户权限 – 基于角色,控制哪些是用户可以看到的,哪些是用户可以做到的。
5。 主题 – 通过模板,以XHTML和CSS的形式对网站的基础数据加以呈现。
(全文 …)

<?
/**
* 生成缩略图
*
* @param string $imagePath 图片路径
* @param string $thumb 生成缩略图名称
* @param integer $width 生成缩略图最大宽度
* @param integer $height 生成缩略图最大高度
*
* @author Silver
* @link http://www.zdyi.com
*/
function resizeImage($imagePath, $thumb, $width = 200, $height = 200)
{
list($imageWidth, $imageHeight) = getimagesize($imagePath);
$imagePath = imagecreatefromjpeg($imagePath);
if ($width && ($imageWidth < $imageHeight))
{
$width = ($height / $imageHeight) * $imageWidth;
}
else
{
$height = ($width / $imageWidth) * $imageHeight;
}
$image = imagecreatetruecolor($width, $height);
imagecopyresampled($image, $imagePath, 0, 0, 0, 0, $width, $height, $imageWidth, $imageHeight);
imagepng($image, $thumb);
imagedestroy($image);
}
resizeImage('test.jpg', 'test_thumb.jpg');
?>