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

针对品鉴网的一些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加入:

PHP代码
  1. 'urlManager' => array(  
  2.     'urlFormat' => 'path',  
  3.     'showScriptName'=>false,  
  4.     'rules' => array(  
  5.         'http://photo.pinjian.net/<action:\w+>'=>'image/<action>',  
  6.         '<controller:\w+>/<id:\d+>' => '<controller>/view',  
  7.         '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',  
  8.         '<controller:\w+>/<action:\w+>' => '<controller>/<action>',  
  9.     ),  
  10. ),  

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

Tags: yii

Unicode转换

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

http://www.kalvin.cn/article/php-encrypt-decrypt-unicode-string-functions-and-escape/
  1. <?php    
  2. function uni_decode($s) {    
  3.     preg_match_all('/\&\#([0-9]{2,5})\;/'$s$html_uni);    
  4.     preg_match_all('/[\\\%]u([0-9a-f]{4})/ie'$s$js_uni);    
  5.     $source = array_merge($html_uni[0], $js_uni[0]);    
  6.     $js = array();    
  7.     for($i=0;$i<count($js_uni[1]);$i++) {    
  8.         $js[] = hexdec($js_uni[1][$i]);    
  9.     }    
  10.     $utf8 = array_merge($html_uni[1], $js);    
  11.     $code = $s;    
  12.     for($j=0;$j<count($utf8);$j++) {    
  13.         $code = str_replace($source[$j], unicode2utf8($utf8[$j]), $code);    
  14.     }    
  15.     return $code;//$s;//preg_replace('/\\\u([0-9a-f]{4})/ie', "chr(hexdec('\\1'))",  $s);    
  16. }    
  17.    
  18. function unicode2utf8($c) {    
  19.     $str="";    
  20.     if ($c < 0x80) {    
  21.          $str.=chr($c);    
  22.     } else if ($c < 0x800) {    
  23.          $str.=chr(0xc0 | $c>>6);    
  24.          $str.=chr(0x80 | $c & 0x3f);    
  25.     } else if ($c < 0x10000) {    
  26.          $str.=chr(0xe0 | $c>>12);    
  27.          $str.=chr(0x80 | $c>>6 & 0x3f);    
  28.          $str.=chr(0x80 | $c & 0x3f);    
  29.     } else if ($c < 0x200000) {    
  30.          $str.=chr(0xf0 | $c>>18);    
  31.          $str.=chr(0x80 | $c>>12 & 0x3f);    
  32.          $str.=chr(0x80 | $c>>6 & 0x3f);    
  33.          $str.=chr(0x80 | $c & 0x3f);    
  34.     }    
  35.     return $str;    
  36. }    
  37.    
  38. $str='%u5927%u5BB6%u597D%uFF0C我是孤魂!<br />\u8FD9\u662F\u6D4B\u8BD5\u6587\u672C\uFF01';    
  39. echo uni_decode($str); // 大家好,我是孤魂!这是测试文本!    
  40. ?>  

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

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上传文件

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

PHP代码
  1. $formAction = 'http://xxx/xxx/xx.upload.php';  
  2. $postVals = array(  
  3.      // /var/www/www.jpg 这个地址是绝对地址哦。可以让程序读取  
  4.      // windows下面可能就是d:/xxx/xxx/xxx.jpg  
  5.      'img'=>'@/var/www/xxx.jpg'//img就相当于表单中的<input type="file" name="img" />  
  6.    
  7. );  
  8. $ch = curl_init();  
  9. curl_setopt($ch, CURLOPT_URL, $formAction); //登录地址  
  10. curl_setopt($ch, CURLOPT_POST, 1); //这是POST数据  
  11. curl_setopt($ch, CURLOPT_POSTFIELDS, ($postVals)); //http_build_query( $postData)); ,如果你真用http_build_query,不一定能成功。所以。。。还是有curl自己去组合吧  
  12. $res = curl_exec($ch);
  13. curl_close($ch);  

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

推荐小程序: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

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

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

 

Tags: thumb