Skip navigation.

极湖

无不用其“极”

Posts tagged with "PHP"

让 Zend_Db 支持 MySQL 的 SQL_CALC_FOUND_ROWS

, ,

修改 Zend/Db/Select.php, 增加以下代码

const FOUND_ROWS     = 'foundrows'; 
const SQL_FOUND_ROWS = 'SQL_CALC_FOUND_ROWS'; 

protected static $_partsInit = array(
    self::FOUND_ROWS   => false,  
    ... ...
);

/**
 * Makes the query SELECT SQL_CALC_FOUND_ROWS.  
 *
 * @param bool $flag Whether or not add SQL_CALC_FOUND_ROWS to SELECT.
 * @return Zend_Db_Select This Zend_Db_Select object.
 */
public function foundRows($flag = true)
{
    $this->_parts[self::FOUND_ROWS] = (bool) $flag;
    return $this;
}

/**
 * Render FOUND_ROWS clause  
 *
 * @param string   $sql SQL query
 * @return string
 */
protected function _renderFoundRows($sql)
{
    if ($this->_parts[self::FOUND_ROWS]) {
        $sql .= ' ' . self::SQL_FOUND_ROWS;
    }

    return $sql;
}


以上代码的具体位置不再详述。

调用方法如下:

$select = $db->select()->foundRows()->from(
  ... ...
)->where(
  ... ...
);

$data = $db->fetchAll($select);

$stmt = $db->query("SELECT FOUND_ROWS() as cnt");
list($rec) = $stmt->fetchAll();
$count = $rec['cnt'];

Windows 下构建 PHP 开发环境: WampServer + Xdebug

,

1. 下载 WampServer
网址: http://www.wampserver.com/en/download.php

2. 安装 WampServer
Windows 的标准安装步骤,连续点“下一步”即可。

3. 下载 Xdebug
网址: http://xdebug.org/download.php

页面上有好几个版本,刚开始一定会被迷惑,不知道该用哪个版本。
经过试验,我的环境(Windows XP)最终能用的是 5.3 VC6 (32 bit)。
(Apache 以外的服务器需用 nts 版本)

下载之后把文件移动(或复制)到 WampServer 的 php 的 ext 目录。

4. 编辑 php.ini
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
追加以下内容:
extension=php_xdebug-2.0.5-5.3-vc6.dll

;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;
追加以下内容:
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000


5. 重新启动 WampServer 的 apache。

Smarty 下最简单的调试方法

, ,

很简单的一句话,却常常忘记,因此记录一下:
{$var|@debug_print_var}

稍微复杂一点的用法:
{$var|@debug_print_var:2:100}

意思是缩进长度为2个空格,每行最多字数为100。

其他调试方法可以参考这个(日文)

在虚拟主机上创建 Symbol Link

, ,

便宜的虚拟主机不提供 Shell,因此需要用命令实现的功能不大容易实现。
做网站经常要用 ln 命令创建链接,而要在一般的虚拟主机上就不得不想其他办法了。
不用命令的情况下,用程序倒也能解决一些问题,比如这个建立链接的目标,可用以下 PHP 程序实现:
<?php
$appLib = realpath(dirname(__FILE__) . '/../library');
$zfLib = realpath(dirname(__FILE__) . '/../../ZF');
symlink("$appLib/Zend", "$zfLib/Zend");

经测试,链接正常建立,然而不能正常工作,只能说虚拟主机的自我保护功能做得还不错。

用于分割字符串的两个 PHP 函数

,

新作了两个函数,结果没用上,放在在这儿以备用。
/**
 * 按词分割字符串
 * @param $str 元字符串
 * @param $maxLength 单词最大长度
 * @return array
 */
function splitInWords($str, $maxLength = 16) {
    if(empty($str)) return array();
    if($maxLength <= 0) $maxLength = 1;
    $rawWords = explode(' ', $str);
    $words = array();
    foreach($rawWords as $word) {
        $len = mb_strlen($word);
        if($len > $maxLength) {
            $pos = 0;
            while($len > $maxLength) {
                $words[] = mb_substr($word, $pos, $maxLength);
                $pos += $maxLength;
                $len -= $maxLength;
            }
            $words[] = mb_substr($word, $pos, $maxLength);
        } else {
            $words[] = $word;
        }
    }
    return $words;
}

/**
 * 按行(指定长度)分割字符串
 * @param $str 元字符串
 * @param $length 每行最大字符数
 * @return array
 */
function splitInLines($str, $length = 80) {
    $lines = array();
    foreach(split("\n", $str) as $s) {
        preg_match_all("/./u", $s, $matches);
        $arr = $matches[0];
        for($i = 0; $i < ceil(count($arr)/$length); $i++) {
            $lines[] = join("", array_slice($arr, $i*$length, $length));
        }
    }
    return $lines;
}

Zend Framework 组件所需的 PHP 扩展模块

, ,

Zend Framework 的组件,不少要用到 PHP 的扩展模块。
详情见:
http://www.mikaelkael.fr/doczf/zh/requirements.extensions.html

我整理了一下其中比较常用的模块,列举如下:

apc
ctype
curl
dom
gd
hash
iconv
json
ldap
libxml
mbstring
memcache
mime_magic
pdo_mysql
pdo_pgsql
pdo_sqlite
posix
Reflection
session
SimpleXML
soap
SPL
Sqlite
xml
zlib

搭建环境,编译 PHP 的时候,最好把以上模块包含进去,以免用到的时候再次编译 PHP。

Zend Framework 重定向方法(render, forward, redirect)总结

,

一. render
不指定render
结果: {当前Module}/{当前Controller}/{当前Action}.phtml

$this->render('bar') ;
结果: {当前Module}/{当前Controller}/bar.phtml

二. forward
$this->_forward('bar') ;
结果: {当前Module}/{当前Controller}/bar

$this->_forward('bar', 'foo') ;
结果: {当前Module}/foo/bar

$this->_forward('bar', 'foo', 'hoge') ;
结果: hoge/foo/bar

$params = array(
'a' => '1',
'b' => '2'
) ;
$this->_forward('bar', 'foo', 'hoge', $params) ;

结果: /hoge/foo/bar/a/1/b/2

三. redirect
$this->_redirect('/hoge') ;
结果: /hoge

$this->_redirect('/hoge/foo') ;
结果: /hoge/foo

$this->_redirect('/hoge/foo/bar') ;
结果: /hoge/foo/bar

$this->_redirect('http://localhost/hoge/foo/bar') ;
结果: http://localhost/hoge/foo/bar

$this->_redirect('http://localhost/hoge/foo/bar?a=1&b=2') ;
结果: http://localhost/hoge/foo/bar?a=1&b=2

四. 特殊情况
不使用 layout
结果: $this->_helper->layout()->disableLayout() ;

不使用 view
结果: $this->_helper->viewRenderer->setNoRender() ;

介绍两个处理 flash 文件的 php 模块

, ,

1. SWF Editor for PHP (swfed)
日本人写的扩展模块, 能实现 swf 文件中的图像替换等功能。

下载: http://sourceforge.jp/projects/swfed/downloads/38169/swfed-0.18.tar.gz

安装:
tar -zxvf swfed-0.18.tar.gz
cd swfed-0.18
phpize
./configure
make
cp modules/swfed.so  /usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/
(注意:以上路径因环境而异)


编辑 php.ini,加入
extension=swfed.so

重新启动 Apache。

2. ffmpeg-php
用于读取视频和音频文件中的信息,能够从视频文件中获取某一帧画面的图片。

首先得安装 ffmpeg
下载: http://ffmpeg.mplayerhq.hu/releases/ffmpeg-0.5.tar.bz2

安装:
tar -jxvf ffmpeg-0.5.tar.bz2
cd ffmpeg-0.5
./configure --enable-shared
make
make install


接下来安装 ffmpeg-php。
下载: http://sourceforge.net/project/downloading.php?group_id=122353&use_mirror=jaist&filename=ffmpeg-php-0.6.0.tbz2

安装:
tar -jxvf ffmpeg-php-0.6.0.tbz2
cd ffmpeg-php-0.6.0
phpize
./configure
make
make install


编辑 php.ini,加入
extension=ffmpeg.so

※补充:
php执行错误:Call to undefined method ffmpeg_frame::toGDImage() 的解决方法:
./configure 执行之后,手动编辑 config.h,加入
#define HAVE_LIBGD20 1
之后再 make clean && make

重新启动 Apache。

没有 SSH 的对策:PHPShell

, ,

闲来没事,更新了一下以前装的 mediawiki,上传新的代码之后,问题产生:数据库等不能通过浏览器更新。

看了一下 mediawiki 的文档,才知道,需要用命令运行 maintenance 目录下的 update.php。

我用的虚拟主机很初级,没有提供 SSH 功能,于是用到了这个叫 PHPShell 的东西。

在服务器上装上 PHPShell 并进行必要的设置之后,在浏览器上就能运行服务器的命令,当然,这只是个简单的模拟,不能和直接用终端相比,不过,能通过浏览器运行命令,目的已经达到了。

PHP 正则表达式常用操作符

,

这东西太常用,有时候又记不住,每次上Google查询,太费事,所以贴一个在这儿。
.(句点)    匹配所有单个字符
^(脱字符号)    匹配出现在行或字符串开头的空字符串
$(美元符号)    匹配出现在行尾的空字符串
A    匹配大写字母 A 
a    匹配小写字母 a 
\d    匹配所有一位数字
\D    匹配所有单个非数字字符
\w    匹配所有单个字母或数字字符;同义词是 [:alnum:] 
[A-E]    匹配所有大写的 A、B、C、D 或 E 
[^A-E]    匹配除大写 A、B、C、D 或 E 之外的任何字符
X?    匹配出现零次或一次的大写字母 X 
X*    匹配零个或多个大写字母 X 
X+    匹配一个或多个大写字母 X 
X{n}    精确匹配 n 个大写字母 X 
X{n,m}    至少匹配 n 个且不多于 m 个大写字母 X;如果忽略 m,则表达式将尝试匹配至少 n 个 X 
(abc|def)+    匹配一连串的(最少一个)abc 和 def;abc 和 def 将匹配

November 2009
S M T W T F S
October 2009December 2009
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30