存档

‘开发’ 分类的存档

正则表达示全集,包括验证汉字在内(转)

2010年7月9日 16hot 没有评论

(“/^[\xa1-\xff]+$/”这是一个纯中文字符串
正则表达式用于字符串处理、表单验证等场合,实用高效。现将一些常用的表达式 收集于此,以备不时之需。

匹配中文字符的正则表达式: [\u4e00-\u9fa5]
评注:匹配中文还真是个头疼的事,有了这个表达式就好办了

匹配双字节字符(包括汉字在内):[^\x00-\xff]
评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计 1)

匹配空白行的正则表达式:\n\s*\r
评注:可以用来删除空白行

匹配HTML标记的正则表达式:<(\S*?)[^>]*>.*?|<.*? />
评注:网上流传的版本太 糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力

匹配首尾空白字符的正则表达式:^\s*|\s*$
评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表 达式

匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
评注:表单验 证时很实用

匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*
评注:网上流传的版本功能很有限,上面这个基本可以满足需求

匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
评注: 表单验证时很实用

匹配国内电话号码:\d{3}-\d{8}|\d{4}-\d{7}
评注:匹配形式如 0511-4405222 或 021-87888822

匹配腾讯QQ号:[1-9][0-9]{4,}
评注:腾讯QQ号从10000开始

匹配中国邮政编码:[1-9]\d{5}(?!\d)
评注:中国邮政编码为6位数字

匹配身份证:\d{15}|\d{18}
评注:中国的身份证为15位或18位

匹配ip地址:\d+\.\d+\.\d+\.\d+
评注:提取ip地址时有用

匹配特定数字:
^[1-9]\d*$    //匹配正整数
^-[1-9]\d*$   //匹配负整数
^-?[1-9]\d*$    //匹配整数
^[1-9]\d*|0$  //匹配非负整数(正整数 + 0)
^-[1-9]\d*|0$   //匹配非正整数(负整数 + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$   //匹配正浮点数
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$   //匹配负浮点数
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$  //匹配浮点数
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$    //匹配非负浮点数(正浮点数 + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$   //匹配非正浮点数(负浮点数 + 0)
评注:处理大量数据时有用,具体应用时注意修正

匹配特定字符串:
^[A-Za-z]+$  //匹配由26个英文字母组成的字符串
^[A-Z]+$  //匹配由26个英文字母 的大写组成的字符串
^[a-z]+$  //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$  //匹配由数字和 26个英文字母组成的字符串
^\w+$  //匹配由数字、26个英文字母或者下划线组成的字符串
评注:最基本也是最常用的一些表达式

分类: 开发 标签:

使用enca转换文件编码

2010年7月8日 16hot 没有评论

安装enca:

# cd /usr/ports/converters/enca
# make install clean

对一个目录下的文件进行转换编码:

# find . -type f | grep -E -v “.svn|.gif|.jpg|.png” | xargs enca -L zh_CN -x UTF-8

分类: BSD/linux, Shell 标签: , ,

php-excel,用PHP导出excel文件

2010年6月7日 16hot 没有评论

php-excel是一个PHP导出excel文件的类库。导出的是XML文件,用office2003,openoffice3都可以正常打开。

官方网站:

http://code.google.com/p/php-excel/

简单的例子:

<?php

// load library
require ‘php-excel.class.php’;

// create a simple 2-dimensional array
$data = array(
1 => array (‘Name’, ‘呵呵’),
array(‘Schwarz’, ‘Oliver’),
array(‘Test’, ‘Peter’)
);

// generate file (constructor parameters are optional)
$xls = new Excel_XML(‘GB2312′, true, ‘My Test Sheet’);
$xls->addArray($data);
$xls->generateXML(‘my-test’);

?>

分类: PHP 标签: , ,

使用rxvt-unicode

2010年5月30日 16hot 没有评论

在新笔记本,迁移到UTF-8环境下,mrxvt不支持UTF-8,中文显示乱码。只有换成rxvt-unicode了。支持UTF-8,中文输入。

配置文件:

$ cat ~/.Xresources
Xft.dpi:96
! Color setting
!URxvt.geometry: 200×53
URxvt.cursorColor:green
URxvt.foreground:black
URxvt.background:lightyellow
! transparent setting
URxvt.inheritPixmap:false
URxvt.tintColor:lightyellow
URxvt.shading:-80
! normal setting
URxvt.termName:rxvt
URxvt.cursorBlink:true
URxvt.saveLines:65535
URxvt.scrollBar_right:true
URxvt.scrollTtyKeypress:true
URxvt.scrollWithBuffer:false
!URxvt.font:xft:serif:pixelsize=13:antialias=false
!URxvt.font: 9x15bold,\
! -misc-fixed-bold-r-normal–15-140-75-75-c-90-iso10646-1,\
! -misc-fixed-medium-r-normal–15-140-75-75-c-90-iso10646-1,\
! [codeset=cp936]xft:serif:antialias=false, \
! xft:simsun:antialias=false

URxvt.font: -*-fixed-medium-r-*–14-*-*-*-*-*-iso10646-1,\
xft:simsun:pixelsize=14:antialias=false

! test modify ALT key value
!URxvt.insecure:true
!URxvt.modifier:alt
!URxvt.meta8:true
!XTerm*VT100*metaSendsEscape:true
urxvt*font:xft:SimSun:pixelsize=16
urxvt*imFont: -misc-simsun-medium-r-normal-0-0-0-0-p-0-iso10646-1
urxvt*inputMethod:SCIM
urxvt*preeditType: OverTheSpot
urxvt*multichar_encoding:noenc

分类: BSD/linux, Shell 标签: ,

shell脚本编译

2010年5月18日 16hot 没有评论

今天发现一个好玩的东东,可以将shell脚本编译一下。

http://www.datsi.fi.upm.es/~frosal/sources/shc.html

文档:

Manpage for shc(1)


NAME

     shc - Generic shell script compiler

SYNOPSIS

     shc [ -e date ] [ -m addr ] [ -i iopt ] [ -x cmnd ]
     [ -l lopt ] [ -ACDhTv ] -f script

DESCRIPTION

     shc creates a stripped  binary  executable  version  of  the
     script specified with -f on the command line.

     The binary version will get a .x extension appended and will
     usually  be  a  bit  larger  in size than the original ascii
     code. Generated C source code is saved in a  file  with  the
     extension .x.c

     If you supply an expiration date with the -e option the com-
     piled  binary  will  refuse to run after the date specified.
     The message "Please contact your provider" will be displayed
     instead.  This message can be changed with the -m option.

     You can compile any kind of shell script, but  you  need  to
     supply valid -i, -x and -l options.

     The compiled binary will still be  dependent  on  the  shell
     specified  in  the  first  line  of  the  shell  code  (i.e.
     #!/bin/sh), thus shc does not create completely  independent
     binaries.

     shc itself is not a compiler such as cc, it  rather  encodes
     and encrypts a shell script and generates C source code with
     the added expiration capability. It  then  uses  the  system
     compiler  to compile a stripped binary which behaves exactly
     like the  original  script.  Upon  execution,  the  compiled
     binary  will  decrypt and execute the code with the shell -c
     option.  Unfortunatelly, it will  not  give  you  any  speed
     improvement as a real C program would.

     shc's main purpose is to protect  your  shell  scripts  from
     modification  or  inspection.  You can use it if you wish to
     distribute your scripts but don't want  them  to  be  easily
     readable by other people.

OPTIONS

     The command line options are:

     -e date
          Expiration date in dd/mm/yyyy format [none]

     -m message
          message to display  upon  expiration  ["Please  contact
          your provider"]

     -f script_name
          File name of the script to compile

     -i inline_option
          Inline option for the shell interpreter i.e: -e

     -x comand
          eXec    command,    as    a    printf    format    i.e:
          exec(\\'%s\\',@ARGV);

     -l last_option
          Last shell option i.e: --

     -r   Relax security. Make  a  redistributable  binary  which
          executes  on different systems running the same operat-
          ing system.

     -v   Verbose compilation

     -D   Switch on debug exec calls

     -T   Allow binary to be  traceable  (using  strace,  ptrace,
          truss, etc.)

     -C   Display license and exit

     -A   Display abstract and exit

     -h   Display help and exit

ENVIRONMENT VARIABLES

     CC   C compiler command [cc]

     CFLAGS
          C compiler flags [none]

EXAMPLES

     Compile a script which can be run on other systems with  the
     trace option enabled:

       example% shc -v -r -T -f myscript

BUGS

     The  maximum  size  of the script that could be executed once com�
     piled is limited by the operating system  configuration  parameter
     _SC_ARG_MAX (see sysconf(2))

AUTHOR

     Francisco Rosales <frosal@fi.upm.es>

REPORT BUGS TO

     the author.
分类: Shell 标签:

一颗璀璨的月光宝石–Lua(转)

2010年5月16日 16hot 没有评论

1993年在巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro in Brazil)诞生了一门编程语言,发明者是该校的三位研究人员,他们给这门语言取了个浪漫的名字——Lua,在葡萄牙语里代表美丽的月亮。事实证明她没 有糟蹋这个优美的单词,Lua语言正如它名字所预示的那样成长为一门简洁、优雅且富有乐趣的语言。

Lua从一开始就是作为一门方便嵌入(其它应用程序)并可扩展的轻量级脚本语言来设计的,因此她一直遵从着简单、小巧、可移植、快速的原则,官 方实现完全采用ANSI C编写,能以C程序库的形式嵌入到宿主程序中。Lua的每个版本都保持着开放源码的传统,不过各版采用的许可协议并不相同,自5.0版(最新版是5.1) 开始她采用的是著名的MIT许可协议。正由于上述特点,所以Lua在游戏开发、机器人控制、分布式应用、图像处理、生物信息学等各种各样的领域中得到了越 来越广泛的应用。其中尤以游戏开发为最,许多著名的游戏,比如Escape from Monkey Island、World of Warcraft、大话西游,都采用了Lua来配合引擎完成数据描述、配置管理和逻辑控制等任务。

作为一门过程型动态语言,Lua有着如下的特性:

1、变量名没有类型,值才有类型,变量名在运行时可与任何类型的值绑定;

2、语言只提供唯一一种数据结构,称为表(table),它类似key-value关联数组,可以用任何类型的值作为key和value。提供 了一致且富有表达力的表构造语法,使得Lua很适合描述复杂的数据;

3、函数是一等类型,支持匿名函数和正则尾递归(proper tail recursion);

4、支持词法定界(lexical scoping)和闭包(closure);

5、提供thread类型和结构化的协程(coroutine)机制,在此基础上可方便实现协作式多任务;

6、运行期能编译字符串形式的程序文本并载入虚拟机执行;

7、通过元表(metatable)和元方法(metamethod)提供动态元机制(dynamic meta-mechanism),从而允许程序运行时根据需要改变或扩充语法设施的内定语义;

8、能方便地利用表和动态元机制实现基于原型(prototype-based)的面向对象模型;

9、从5.1版开始提供了完善的模块机制,从而更好地支持开发大型的应用程序;

Lua的语法类似PASCAL和Modula但更加简洁,所有的语法产生式规则(EBNF)不过才60几个。熟悉C和PASCAL的程序员一般 只需半个小时便可将其完全掌握。而在语义上Lua则与Scheme极为相似,她们完全共享上述的1、3、4、6点特性, Scheme的continuation与协程也基本相同只是自由度更高。最引人注目的是,两种语言都只提供唯一一种数据结构:Lua的表和Scheme 的列表(list)。正因为如此,有人甚至称Lua为“只用表的Scheme”。 阅读全文…

分类: LUA, 转载 标签:

jQuery操作input(转载)

2010年5月5日 16hot 没有评论
jQuery操作input
2009-10-14 15:10

获取选中的值
获取一组radio 被选中项的值
var item = $(‘input[@name=items][@checked]‘).val();

获取select被选中项的文本
var item = $(“select[@name=items] option[@selected]“).text();

select下拉框的第二个元素为当前选中值
$(‘#select_id’)[0].selectedIndex = 1;

radio单选组的第二个元素为当前选中值
$(‘input[@name=items]‘).get(1).checked = true;

获取值:
文本框,文本区域:
$(“#txt”).attr(“value”);

$(“#txt”).val();

多选框checkbox:
$(“#checkbox_id”).attr(“value”);

单选组radio:
$(“input[@type=radio][@checked]“).val();

下拉框select:
$(‘#sel’).val();

控制表单元素:
文本框,文本区域:

$(“#txt”).attr(“value”,”);//清空内容
$(“#txt”).attr(“value”,’11′);//填充内容

多选框checkbox:
$(“#chk1″).attr(“checked”,”);//不打勾
$(“#chk2″).attr(“checked”,true);//打勾
if($(“#chk1″).attr(‘checked’)==undefined) //判断是否已经打勾

单选组radio:
$(“input[@type=radio]“).attr(“checked”,’2′);//设置 value=2的项目为当前选中项

下拉框select:
$(“#sel”).attr(“value”,’-sel3′);//设置value=-sel3的项目为当前选中项
$(“<optionvalue=’1′>1111</option><optionvalue=’2′> 2222</option>”).appendTo(“#sel”)//添加下拉框的option
$(“#sel”).empty();//清空下拉框

=====================

在 Jquery中,用$(“#id”)来获得页面的input元素, 其相当于document.getElementById(“element”)
但是,该获取 的是一个Jquery对象,而不是一个dom element对象.value是dom element对象的属性.所以,使用
$(“#id”).value 不能取到值
取值的方法如 下:
取值:val = $(“#id”)[0].value;
$(“#id”)[0].value = “new value”;
赋值:
$(“#id”)[0].value = “new value”;
或者$(“#id”).val(“new value”);

val = $(“#id”).attr(“value”);

阅读全文…

分类: PHP, 转载 标签:

PHP生成条码

2010年4月30日 16hot 没有评论

使用PHP来生成条码,找到了一个开源的程序php-barcode。

网站首页: http://www.ashberg.de/php-barcode/index.php

下载地址: http://www.ashberg.de/php-barcode/download/

需要安装barcodegenbarcode。我是在FreeBSD下安装的。barcode就直接在ports里安装了。

安装barcode:

# cd /usr/ports/graphics/barcode
# make install clean

安装genbarcode:

# tar -zxf genbarcode-0.4.tar.gz
# cd genbarcode-0.4
# gmake
# gmake install

安装php-barcode:

在WEB目录下:

# tar -zxf php-barcode-0.3pl1.tar.gz
# mv php-barcode-0.3pl1 phpbarcode
# chmod -R 755 phpbarcode

访问:

* http://localhost/barcode.php?code=012345678901
* http://localhost/barcode.php?code=012345678901&encoding=EAN&scale=4&mode=png

条码图

分类: PHP 标签:

在FreeBSD 安装php-fpm 5.2.12

2010年4月2日 16hot 没有评论

php-fpm的freebsd ports只到5.2.11就没有更新了。但是其他php的扩展都升级到了5.2.12。调试程序的时候出现异常情况。就自己动手修改了php-fpm的ports配置文件。

Makefile 补丁:

— ../php5-fpm/Makefile        2009-10-13 05:53:43.000000000 +0800
+++ Makefile    2010-04-02 21:08:21.000000000 +0800
@@ -7,7 +7,7 @@

PORTNAME=      php5-fpm
PORTVERSION=   ${PHP_VERSION}
-PHP_VERSION=   5.2.11
+PHP_VERSION=   5.2.12
PHPFPM_VERSION= 0.6.3
PORTREVISION?= 0
CATEGORIES?=   lang devel www

distinfo补丁:

— ../php5-fpm/distinfo        2009-10-13 05:41:42.000000000 +0800
+++ distinfo    2010-04-02 21:13:46.000000000 +0800
@@ -1,12 +1,12 @@
-MD5 (php-5.2.11.tar.bz2) = 286bf34630f5643c25ebcedfec5e0a09
-SHA256 (php-5.2.11.tar.bz2) = 9bcd14ceda2b4bd7abcc7eb59bd74bae490d9335e4207580de783b48aa7e8f23
-SIZE (php-5.2.11.tar.bz2) = 9030787
+MD5 (php-5.2.12.tar.bz2) = 5b7077e366c7eeab34da31dd860a1923
+SHA256 (php-5.2.12.tar.bz2) = e65756a8412726a491ca48da1e0693eaeb3f38f19fb6cbc8f53005cab1f2491a
+SIZE (php-5.2.12.tar.bz2) = 9075161
MD5 (php-fpm-0.6.3.tar.bz2) = be0d67a73474f2273b8d90447d057fc2
SHA256 (php-fpm-0.6.3.tar.bz2) = 1233ce7b19c90e159474af13548541617ceead85887b201f752bc1bbbe3e4905
SIZE (php-fpm-0.6.3.tar.bz2) = 1909302
-MD5 (suhosin-patch-5.2.11-0.9.7.patch.gz) = 8f9de4d97fae6eba163cf3699509a260
-SHA256 (suhosin-patch-5.2.11-0.9.7.patch.gz) = 392f10c9b7d9c47f30e989fb7775cc46d36153b933bf7ac9ccd8826b2954584b
-SIZE (suhosin-patch-5.2.11-0.9.7.patch.gz) = 23050
+MD5 (suhosin-patch-5.2.12-0.9.7.patch.gz) = 40be1b05ad893a01778d7fb323dd8872
+SHA256 (suhosin-patch-5.2.12-0.9.7.patch.gz) = f324cf09cb83826fb35308c890ca06e14d64e5d65129f3ba8f3e61953e442366
+SIZE (suhosin-patch-5.2.11-0.9.7.patch.gz) = 23062
MD5 (php-5.2.10-mail-header.patch) = 7f73682e78d32e22989c3fb3678d668b
SHA256 (php-5.2.10-mail-header.patch) = a61d50540f4aae32390118453845c380fe935b6d1e46cef6819c8561946e942f
SIZE (php-5.2.10-mail-header.patch) = 3383

在编译安装的过程中,会报libevent错误:

../libevent/.libs/libevent.a: could not read symbols: File format not recognized
*** Error code 1

Stop in /usr/ports/lang/php5-fpm/work/php-fpm-0.6.3/src.
*** Error code 1

Stop in /usr/ports/lang/php5-fpm/work/php-fpm-0.6.3/src.
*** Error code 1

Stop in /usr/ports/lang/php5-fpm/work/php-fpm-0.6.3/src.
*** Error code 1

Stop in /usr/ports/lang/php5-fpm/work/php-fpm-0.6.3.
*** Error code 1

解决办法:

# cd work/php-fpm-0.6.3/libevent/
# make clean
# cd ../../../
# make install clean

分类: BSD/linux, PHP 标签: , ,

nginx && libevent 的一些链接

2009年11月12日 16hot 没有评论

最近两天在学习nginx,顺便也学习了下libevent。将相关的一些技术文章地址摘录如下:

http://night9.cn/category/nginx

http://amix.dk/blog/viewEntry/19414

http://www.igvita.com/2008/02/11/nginx-and-memcached-a-400-boost/

http://wiki.nginx.org/Nginx3rdPartyModules

http://code.google.com/p/php-redis/

http://blog.sina.com.cn/iyangjian

 如何写nginx module

http://timyang.net/web/nginx-module/

http://code.google.com/p/redis/

 

另外,计划使用libevent改写扫描和发送报警引擎。

分类: C/C++ 标签: