PHP

浏览模式: 标准 | 列表 | 图文
yiilog.com

yiilog.com

YiiLog.com近日起即将启用。
之所以注册这个域名,是因为使用Yii来,用它开发了不少的东西,也积累了一些代码
有部分代码不能公开,也有收集、收藏了别人的一些代码(基于Yii的)。
嗯。也有一些API也会用YiiLog来发布。

api.YiiLog.com,将是用来对外发布服务的。(该服务将会使用SAE平台,因为速度会更快一点)
YiiLog.com主站还是会扔在这台VPS上。慢就慢一点,但毕竟是自己可控制的

xcache 发布3.0版本

xcache 发布3.0版本

xcache也开始追求版本号了?
1.x的时候几年都没变。2.01刚发布3个月立刻就是3.0了?
虽然官网上说这是一个里程碑式的更新,但谁能说的清?

更新倒是不少:

API Changes
========
* chg: proto array xcache_clear_cache(int type, [ int id = -1 ]). -1 means all cache splits
* new: proto array xcache_enable_cache(int type, [ int id = -1, [ bool enable = true ] ])
* new: proto array xcache_admin_namespace()
* new: proto array xcache_set_namespace(string namespace)

Ini Settings Changes
========
* new: xcache.disable_on_crash = Off
* new: xcache.coverager_autostart = On
* new: xcache.allocator = bestfit (no other option value yet)
* new: xcache.var_allocator = bestfit (no other option value yet)

ChangeLog
========
* closes #2: auto disable caching on crash
* closes #73: warn for improper PHP_FCGI_CHILDREN setting fcgi mode (>=PHP_5_3)
* closes #174: updates api to support "clear all cache"
* closes #198: support for caching protocol url
* closes #287: namespace support
* fixes #39: ini_set never work for xcache.coverager setting. use API instead
* code refactor
* uses extension= to load XCache. loading via zend_extension= is unsupported
* split XCache features into multiple sub modules
* updates XCache admin page
* adds an diagnosis module to give professional advise, accessable via htdocs pages
* cache can be enabled/disabled manually

反正,他们自我评价挺高的:XCache 3.0.0 released. Lots of improvements, bug fixes. This is a big new milestone refactoring most XCache code.

在首页的下载里面还写着:
2.1.x, unstable, devel, features
结果,3.0都Release了。。。
这年头。。。还能怎么办?

针对品鉴网的一些URL路由设置

针对品鉴网的一些URL路由设置

这篇文章又能当成开发文档,也能当成软文,所以,你们就将就着看吧。

yii框架中很早就支持路由功能了。所以,从那时候开始,很多人就开始将项目路由化,举例说明:
http://user.pinjian.net,访问用户中心
http://admin.pinjian.net 访问后台
http://xxxx.pinjian.net 访问Xxx
表面上这是一大堆 网站,其实很有可能是什么?只是一个控制器或者一个module罢了。
http://user.pinjian.net可能对应了什么?http://pinjian.net/user/index,很有可能就这样而已。
而对于yii来说,这个太简单了
在main.php(环境 变量设置中),针对urlManager的rules加入:

'urlManager' => array(
'urlFormat' => 'path',
'showScriptName'=>false,
'rules' => array(
'http://photo.pinjian.net/<action:\w+>'=>'image/<action>',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
),

看看高亮的那一行,是不是很简单?
之前,有一篇博客介绍过的:http://www.neatstudio.com/show-1550-1.shtml,还篇比较详细

Unicode转换

其实只是一段很少的代码。

<?php
function uni_decode($s) {
preg_match_all('/\&\#([0-9]{2,5})\;/', $s, $html_uni);
preg_match_all('/[\\\%]u([0-9a-f]{4})/ie', $s, $js_uni);
$source = array_merge($html_uni[0], $js_uni[0]);
$js = array();
for($i=0;$i<count($js_uni[1]);$i++) {
$js[] = hexdec($js_uni[1][$i]);
}
$utf8 = array_merge($html_uni[1], $js);
$code = $s;
for($j=0;$j<count($utf8);$j++) {
$code = str_replace($source[$j], unicode2utf8($utf8[$j]), $code);
}
return $code;//$s;//preg_replace('/\\\u([0-9a-f]{4})/ie', "chr(hexdec('\\1'))", $s);
}

function unicode2utf8($c) {
$str="";
if ($c < 0x80) {
$str.=chr($c);
} else if ($c < 0x800) {
$str.=chr(0xc0 | $c>>6);
$str.=chr(0x80 | $c & 0x3f);
} else if ($c < 0x10000) {
$str.=chr(0xe0 | $c>>12);
$str.=chr(0x80 | $c>>6 & 0x3f);
$str.=chr(0x80 | $c & 0x3f);
} else if ($c < 0x200000) {
$str.=chr(0xf0 | $c>>18);
$str.=chr(0x80 | $c>>12 & 0x3f);
$str.=chr(0x80 | $c>>6 & 0x3f);
$str.=chr(0x80 | $c & 0x3f);
}
return $str;
}

$str='%u5927%u5BB6%u597D%uFF0C我是孤魂!<br />\u8FD9\u662F\u6D4B\u8BD5\u6587\u672C\uFF01';
echo uni_decode($str); // 大家好,我是孤魂!这是测试文本!
?>

这段代码以前也写过,而且,其实用的地方挺多,比如json的字符串(有些只是json处理了一下,但并不能完全转换,总不能强制变成json格式再转换吧?)
其实黑的很方便,而且这段代码在PHP手册中的注释里也出现过,但一下子找不到了,于是看到后就做了个备份罢了

version_compare

version_compare

在项目中遇到点小问题。。。
比如:1.4.0与1.4.1对比。还有1.2与1.21对比
在初期的时候,我都是直接使用version_compare来对比的,但这时候就出现了问题

在国人眼中,1.21其实是1.2的后续版本,它是比1.3小的一个版本。但。。。version_compare即不认的。它认为1.21>1.2,当然1.21也>1.3。。。
好纠结啊。。。

后面没办法,对于1.4.0这种用version_compare了。对于1.21的,直接浮点数来比较了。NND

以后对版本号还是需要有一个规划,不能乱写啊

利用curl上传文件

利用curl上传文件

利用php的curl库上传文件真的很方便
注意几点
1、只能是POST
2、可以不写头(平时的form有文件和无文件时发送的时候,)
3、postfields的时候,不要你傻傻的http_build_query,你越是这么做了。黑黑。。。就反而不一定对了
4、上传的文件用@来连接。
一个很小的例子

$formAction = 'http://xxx/xxx/xx.upload.php';
$postVals = array(
// /var/www/www.jpg 这个地址是绝对地址哦。可以让程序读取
// windows下面可能就是d:/xxx/xxx/xxx.jpg
'img'=>'@/var/www/xxx.jpg', //img就相当于表单中的<input type="file" name="img" />

);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $formAction); //登录地址
curl_setopt($ch, CURLOPT_POST, 1); //这是POST数据
curl_setopt($ch, CURLOPT_POSTFIELDS, ($postVals)); //http_build_query( $postData)); ,如果你真用http_build_query,不一定能成功。所以。。。还是有curl自己去组合吧
$res = curl_exec($ch);
curl_close($ch);

看看多简单,一下子就成功了

推荐小程序:TimThumb – PHP Image Resizer

推荐小程序:TimThumb – PHP Image Resizer

这是一个单文件处理缩略图的程序,直接引用生成缩略图。有点意思,主要是方便啦 .。

官方地址是:http://www.binarymoon.co.uk/projects/timthumb/
文件也很小,只有一点点大,更关键的是可以缓存回来。也能取远程的图片,也可以设置allow的安全站点,这样就相对安全一点了,官方这么介绍 :
TimThumb is a simple, flexible, PHP script that resizes images. You give it a bunch of parameters, and it spits out a thumbnail image that you can display on your site.

TimThumb has seen a massive amount of use across the WordPress world, and a few months after we released it, I took over development from Tim, a friend of Darren Hoyts, who created the original script. Since I took over there have been a whole host of changes, bug fixes, and additions.

I set up this page to act as an archive/ resource, showing the best documentation and demos available for TimThumb. If you have any TimThumb related articles then please feel free to send them over to me so that I can add them to the list.

功能也相对比较方便,用法也很简单,官方现成就举了一些例子,并通过这些例子介绍了它的参数:

How to use TimThumb

看了上面的使用方法,再看看,这个,你就知道很多常见的方法和用法了:

stands for values What it does
src source url to image Tells TimThumb which image to resize › tutorial
w width the width to resize to Remove the width to scale proportionally (will then need the height) › tutorial
h height the height to resize to Remove the height to scale proportionally (will then need the width) › tutorial
q quality 0 - 100 Compression quality. The higher the number the nicer the image will look. I wouldn't recommend going any higher than about 95 else the image will get too large › tutorial
a alignment c, t, l, r, b, tl, tr, bl, br Crop alignment. c = center, t = top, b = bottom, r = right, l = left. The positions can be joined to create diagonal positions › tutorial
zc zoom / crop 0, 1, 2, 3 Change the cropping and scaling settings › tutorial
f filters too many to mention Let's you apply image filters to change the resized picture. For instance you can change brightness/ contrast or even blur the image › tutorial
s sharpen Apply a sharpen filter to the image, makes scaled down images look a little crisper › tutorial
cc canvas colour hexadecimal colour value (#ffffff) Change background colour. Most used when changing the zoom and crop settings, which in turn can add borders to the image.
ct canvas transparency true (1) Use transparency and ignore background colour

参数很简单,就是不太好记。。。纠结啊

不过用起来的话,是真心简单

旧文:QeePHP中的优秀函数(三)

旧文:QeePHP中的优秀函数(三)

这几个函数还是来自于QeePHP的核心类Q中。不过,我是自认为,我的configure类有部分写的比他好,不过我没有考虑删除之类的。呵呵。

/**
* 获取指定的设置内容
*
* $option 参数指定要获取的设置名。
* 如果设置中找不到指定的选项,则返回由 $default 参数指定的值。
*
* @code php
* $option_value = Q::ini('my_option');
* @endcode
*
* 对于层次化的设置信息,可以通过在 $option 中使用“/”符号来指定。
*
* 例如有一个名为 option_group 的设置项,其中包含三个子项目。
* 现在要查询其中的 my_option 设置项的内容。
*
* @code php
* // +--- option_group
* // +-- my_option = this is my_option
* // +-- my_option2 = this is my_option2
* // \-- my_option3 = this is my_option3
*
* // 查询 option_group 设置组里面的 my_option 项
* // 将会显示 this is my_option
* echo Q::ini('option_group/my_option');
* @endcode
*
* 要读取更深层次的设置项,可以使用更多的“/”符号,但太多层次会导致读取速度变慢。
*
* 如果要获得所有设置项的内容,将 $option 参数指定为 '/' 即可:
*
* @code php
* // 获取所有设置项的内容
* $all = Q::ini('/');
* @endcode
*
* @param string $option 要获取设置项的名称
* @param mixed $default 当设置不存在时要返回的设置默认值
*
* @return mixed 返回设置项的值
*/
static function ini($option, $default = null)
{
if ($option == '/') return self::$_ini;

if (strpos($option, '/') === false)
{
return array_key_exists($option, self::$_ini)
? self::$_ini[$option]
: $default;
}

$parts = explode('/', $option);
$pos =& self::$_ini;
foreach ($parts as $part)
{
if (!isset($pos[$part])) return $default;
$pos =& $pos[$part];
}
return $pos;
}

/**
* 修改指定设置的内容
*
* 当 $option 参数是字符串时,$option 指定了要修改的设置项。
* $data 则是要为该设置项指定的新数据。
*
* @code php
* // 修改一个设置项
* Q::changeIni('option_group/my_option2', 'new value');
* @endcode
*
* 如果 $option 是一个数组,则假定要修改多个设置项。
* 那么 $option 则是一个由设置项名称和设置值组成的名值对,或者是一个嵌套数组。
*
* @code php
* // 假设已有的设置为
* // +--- option_1 = old value
* // +--- option_group
* // +-- option1 = old value
* // +-- option2 = old value
* // \-- option3 = old value
*
* // 修改多个设置项
* $arr = array(
* 'option_1' => 'value 1',
* 'option_2' => 'value 2',
* 'option_group/option2' => 'new value',
* );
* Q::changeIni($arr);
*
* // 修改后
* // +--- option_1 = value 1
* // +--- option_2 = value 2
* // +--- option_group
* // +-- option1 = old value
* // +-- option2 = new value
* // \-- option3 = old value
* @endcode
*
* 上述代码展示了 Q::changeIni() 的一个重要特性:保持已有设置的层次结构。
*
* 因此如果要完全替换某个设置项和其子项目,应该使用 Q::replaceIni() 方法。
*
* @param string|array $option 要修改的设置项名称,或包含多个设置项目的数组
* @param mixed $data 指定设置项的新值
*/
static function changeIni($option, $data = null)
{
if (is_array($option))
{
foreach ($option as $key => $value)
{
self::changeIni($key, $value);
}
return;
}

if (!is_array($data))
{
if (strpos($option, '/') === false)
{
self::$_ini[$option] = $data;
return;
}

$parts = explode('/', $option);
$max = count($parts) - 1;
$pos =& self::$_ini;
for ($i = 0; $i < = $max; $i ++)
{
$part = $parts[$i];
if ($i < $max)
{
if (!isset($pos[$part]))
{
$pos[$part] = array();
}
$pos =& $pos[$part];
}
else
{
$pos[$part] = $data;
}
}
}
else
{
foreach ($data as $key => $value)
{
self::changeIni($option . '/' . $key, $value);
}
}
}

/**
* 替换已有的设置值
*
* Q::replaceIni() 表面上看和 Q::changeIni() 类似。
* 但是 Q::replaceIni() 不会保持已有设置的层次结构,
* 而是直接替换到指定的设置项及其子项目。
*
* @code php
* // 假设已有的设置为
* // +--- option_1 = old value
* // +--- option_group
* // +-- option1 = old value
* // +-- option2 = old value
* // \-- option3 = old value
*
* // 替换多个设置项
* $arr = array(
* 'option_1' => 'value 1',
* 'option_2' => 'value 2',
* 'option_group/option2' => 'new value',
* );
* Q::replaceIni($arr);
*
* // 修改后
* // +--- option_1 = value 1
* // +--- option_2 = value 2
* // +--- option_group
* // +-- option2 = new value
* @endcode
*
* 从上述代码的执行结果可以看出 Q::replaceIni() 和 Q::changeIni() 的重要区别。
*
* 不过由于 Q::replaceIni() 速度比 Q::changeIni() 快很多,
* 因此应该尽量使用 Q::replaceIni() 来代替 Q::changeIni()。
*
* @param string|array $option 要修改的设置项名称,或包含多个设置项目的数组
* @param mixed $data 指定设置项的新值
*/
static function replaceIni($option, $data = null)
{
if (is_array($option))
{
self::$_ini = array_merge(self::$_ini, $option);
}
else
{
self::$_ini[$option] = $data;
}
}

/**
* 删除指定的设置
*
* Q::cleanIni() 可以删除指定的设置项目及其子项目。
*
* @param mixed $option 要删除的设置项名称
*/
static function cleanIni($option)
{
if (strpos($option, '/') === false)
{
unset(self::$_ini[$option]);
}
else
{
$parts = explode('/', $option);
$max = count($parts) - 1;
$pos =& self::$_ini;
for ($i = 0; $i < = $max; $i ++)
{
$part = $parts[$i];
if ($i < $max)
{
if (!isset($pos[$part]))
{
$pos[$part] = array();
}
$pos =& $pos[$part];
}
else
{
unset($pos[$part]);
}
}
}
}
旧文:QeePHP中的优秀函数(二)

旧文:QeePHP中的优秀函数(二)

这两个函数来自于Helper_Array,我觉得是非常常用的方法,功能也比较强大。适合大家使用。

/**
* 将一个平面的二维数组按照指定的字段转换为树状结构
*
* 用法:
* @code php
* $rows = array(
* array('id' => 1, 'value' => '1-1', 'parent' => 0),
* array('id' => 2, 'value' => '2-1', 'parent' => 0),
* array('id' => 3, 'value' => '3-1', 'parent' => 0),
*
* array('id' => 7, 'value' => '2-1-1', 'parent' => 2),
* array('id' => 8, 'value' => '2-1-2', 'parent' => 2),
* array('id' => 9, 'value' => '3-1-1', 'parent' => 3),
* array('id' => 10, 'value' => '3-1-1-1', 'parent' => 9),
* );
*
* $tree = Helper_Array::tree($rows, 'id', 'parent', 'nodes');
*
* dump($tree);
* // 输出结果为:
* // array(
* // array('id' => 1, ..., 'nodes' => array()),
* // array('id' => 2, ..., 'nodes' => array(
* // array(..., 'parent' => 2, 'nodes' => array()),
* // array(..., 'parent' => 2, 'nodes' => array()),
* // ),
* // array('id' => 3, ..., 'nodes' => array(
* // array('id' => 9, ..., 'parent' => 3, 'nodes' => array(
* // array(..., , 'parent' => 9, 'nodes' => array(),
* // ),
* // ),
* // )
* @endcode
*
* 如果要获得任意节点为根的子树,可以使用 $refs 参数:
* @code php
* $refs = null;
* $tree = Helper_Array::tree($rows, 'id', 'parent', 'nodes', $refs);
*
* // 输出 id 为 3 的节点及其所有子节点
* $id = 3;
* dump($refs[$id]);
* @endcode
*
* @param array $arr 数据源
* @param string $key_node_id 节点ID字段名
* @param string $key_parent_id 节点父ID字段名
* @param string $key_childrens 保存子节点的字段名
* @param boolean $refs 是否在返回结果中包含节点引用
*
* return array 树形结构的数组
*/
static function toTree($arr, $key_node_id, $key_parent_id = 'parent_id',
$key_childrens = 'childrens', & $refs = null)
{
$refs = array();
foreach ($arr as $offset => $row)
{
$arr[$offset][$key_childrens] = array();
$refs[$row[$key_node_id]] =& $arr[$offset];
}

$tree = array();
foreach ($arr as $offset => $row)
{
$parent_id = $row[$key_parent_id];
if ($parent_id)
{
if (!isset($refs[$parent_id]))
{
$tree[] =& $arr[$offset];
continue;
}
$parent =& $refs[$parent_id];
$parent[$key_childrens][] =& $arr[$offset];
}
else
{
$tree[] =& $arr[$offset];
}
}

return $tree;
}

/**
* 将树形数组展开为平面的数组
*
* 这个方法是 tree() 方法的逆向操作。
*
* @param array $tree 树形数组
* @param string $key_childrens 包含子节点的键名
*
* @return array 展开后的数组
*/
static function treeToArray($tree, $key_childrens = 'childrens')
{
$ret = array();
if (isset($tree[$key_childrens]) && is_array($tree[$key_childrens]))
{
$childrens = $tree[$key_childrens];
unset($tree[$key_childrens]);
$ret[] = $tree;
foreach ($childrens as $node)
{
$ret = array_merge($ret, self::treeToArray($node, $key_childrens));
}
}
else
{
unset($tree[$key_childrens]);
$ret[] = $tree;
}
return $ret;
}
不过显而易见,这两个函数,都不需要多介绍,tree2list,list2tree,想想也知道怎么用,再加上注释又比较全。
可惜QeePHP不再开发,而ThinkPHP积下来的问题又很多,改动起来也非常痛苦。所以我开始慢慢分析一下。
旧文:QeePHP中的优秀函数(一)

旧文:QeePHP中的优秀函数(一)

基类Q中的normalize。

/**
* 对字符串或数组进行格式化,返回格式化后的数组
*
* $input 参数如果是字符串,则首先以“,”为分隔符,将字符串转换为一个数组。
* 接下来对数组中每一个项目使用 trim() 方法去掉首尾的空白字符。最后过滤掉空字符串项目。
*
* 该方法的主要用途是将诸如:“item1, item2, item3” 这样的字符串转换为数组。
*
* @code php
* $input = 'item1, item2, item3';
* $output = Q::normalize($input);
* // $output 现在是一个数组,结果如下:
* // $output = array(
* // 'item1',
* // 'item2',
* // 'item3',
* // );
*
* $input = 'item1|item2|item3';
* // 指定使用什么字符作为分割符
* $output = Q::normalize($input, '|');
* @endcode
*
* @param array|string $input 要格式化的字符串或数组
* @param string $delimiter 按照什么字符进行分割
*
* @return array 格式化结果
*/
static function normalize($input, $delimiter = ',')
{
if (!is_array($input))
{
$input = explode($delimiter, $input);
}
$input = array_map('trim', $input);
return array_filter($input, 'strlen');
}
看到这个方法,其实应该感觉得到它的用户,确实,大多数情况下,我们都是用explode来分割字符串的,但事实上,你不能保证别人给你的字符串有没有多 余的空格,或者无效的字符串,因此,通过这个函数,就可以去除掉很无效的值 。建议使用它来代替explode,但,如果确实是很规则的,还是用explode吧,毕竟,如果这个分割出来的数组很大,效率肯定比explode低很 多了。