订阅到抓虾 Add to netvibes 添加到Google Reader(阅读器) Hosted by exlast.com

Archive for the 'Programming' Category

图形文件格式 PPM 介绍

在做的一个项目中需要用到这种文件格式 .ppm 。网上相关的介绍并不多。摘录一篇备忘。

A PPM file consists of two parts, a header and the image data. The header consists of at least three parts normally delinineated by carriage returns and/or linefeeds but the PPM specification only requires white space. The first “line” is a magic PPM identifier, it can be “P3″ or “P6″ (not including the double quotes!). The next line consists of the width and height of the image as ascii numbers. The last part of the header gives the maximum value of the colour components for the pixels, this allows the format to describe more than single byte (0..255) colour values. In addition to the above required lines, a comment can be placed anywhere with a “#” character, the comment extends to the end of the line.

The following are all valid PPM headers.

Header example 1
P6 1024 788 255

Header example 2
P6
1024 788
# A comment
255

Header example 3
P3
1024 # the image width
788 # the image height
# A comment
1023

The format of the image data itself depends on the magic PPM identifier. If it is “P3″ then the image is given as ascii text, the numerical value of each pixel ranges from 0 to the maximum value given in the header. The lines should not be longer than 70 characters.

PPM example 4
P3
# example from the man page
4 4
15
0 0 0 0 0 0 0 0 0 15 0 15
0 0 0 0 15 7 0 0 0 0 0 0
0 0 0 0 0 0 0 15 7 0 0 0
15 0 15 0 0 0 0 0 0 0 0 0

If the PPM magic identifier is “P6″ then the image data is stored in byte format, one byte per colour component (r,g,b). Comments can only occur before the last field of the header and only one byte may appear after the last header field, normally a carriage return or line feed. “P6″ image files are obviously smaller than “P3″ and much faster to read. Note that “P6″ PPM files can only be used for single byte colours.

While not required by the format specification it is a standard convention to store the image in top to bottom, left to right order. Each pixel is stored as a byte, value 0 == black, value 255 == white. The components are stored in the “usual” order, red - green - blue.

图形文件格式列表

Tags: ,

每个web程序员都应该知道的5个正则表达式

1、匹配用户名

规则:

  • 允许字符和数字(a-z,A-Z,0-9)
  • 允许下划线

正则表达式:

/^[a-zA-Z0-9_]{3,16}$/

代码示例:

function validate_username( $username ) {
if(preg_match(’/^[a-zA-Z0-9_]{3,16}$/’, $_GET['username'])) {
return true;
}
return false;
}

2、匹配XHTML或XML标签

正则表达式:

{]*>(.*?)}

代码示例:

function get_tag( $tag, $xml ) {
$tag = preg_quote($tag);
preg_match_all(’{<'.$tag.'[^>]*>(.*?).’}',
$xml,
$matches,
PREG_PATTERN_ORDER);

return $matches[1];
}

3、匹配确定属性值的XHTML或XML标签(例如:class或tag)

正则表达式:

{]*attribute\\s*=\\s*(["'])value\\\\1[^>]*>(.*?)}

代码示例:

function get_tag( $attr, $value, $xml, $tag=null ) {
if( is_null($tag) )
$tag = ‘\\w+’;
else
$tag = preg_quote($tag);

$attr = preg_quote($attr);
$value = preg_quote($value);

$tag_regex = “/<(".$tag.")[^>]*$attr\\s*=\\s*”.
“(['\\"])$value\\\\2[^>]*>(.*?)<\\/\\\\1>/”

preg_match_all($tag_regex,
$xml,
$matches,
PREG_PATTERN_ORDER);

return $matches[3];
}

4、匹配和解析email地址

代码示例(比较复杂些):

function is_valid_email_address($email){
$qtext = ‘[^\\x0d\\x22\\x5c\\x80-\\xff]‘;
$dtext = ‘[^\\x0d\\x5b-\\x5d\\x80-\\xff]‘;
$atom = ‘[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+’;
$quoted_pair = ‘\\x5c[\\x00-\\x7f]‘;
$domain_literal = “\\x5b($dtext|$quoted_pair)*\\x5d”;
$quoted_string = “\\x22($qtext|$quoted_pair)*\\x22″;
$domain_ref = $atom;
$sub_domain = “($domain_ref|$domain_literal)”;
$word = “($atom|$quoted_string)”;
$domain = “$sub_domain(\\x2e$sub_domain)*”;
$local_part = “$word(\\x2e$word)*”;
$addr_spec = “$local_part\\x40$domain”;

return preg_match(”!^$addr_spec$!”, $email) ? 1 : 0;
}

5、匹配URL

正则表达式:

{
\\b
# Match the leading part (proto://hostname, or just hostname)
(
# http://, or https:// leading part
(https?)://[-\\w]+(\\.\\w[-\\w]*)+
|
# or, try to find a hostname with more specific sub-expression
(?i: [a-z0-9] (?:[-a-z0-9]*[a-z0-9])? \\. )+ # sub domains
# Now ending .com, etc. For these, require lowercase
(?-i: com\\b
| edu\\b
| biz\\b
| gov\\b
| in(?:t|fo)\\b # .int or .info
| mil\\b
| net\\b
| org\\b
| [a-z][a-z]\\.[a-z][a-z]\\b # two-letter country code
)
)

# Allow an optional port number
( : \\d+ )?

# The rest of the URL is optional, and begins with /
(
/
# The rest are heuristics for what seems to work well
[^.!,?;"\\'<>()\[\]\{\}\s\x7F-\\xFF]*
(
[.!,?]+ [^.!,?;"\\'<>()\\[\\]\{\\}\s\\x7F-\\xFF]+
)*
)?
}ix

完整的文章请访问这里阅读(英文)

Tags:

解决方案:CakePHP中文乱码问题

发现不少人在初学 CakePHP 时遇到中文乱码的问题。如图:

首先需要确认的是,你mysql库里面的编码设置都是utf-8

解决方案很简单,就是在数据库配置文件(/app/config/database.php)里加一句
Continue reading ‘解决方案:CakePHP中文乱码问题’

Tags: , ,

40个迹象表明你还是PHP菜鸟

  • 1. 不会利用如phpDoc这样的工具来恰当地注释你的代码
  • 2. 对优秀的集成开发环境如Zend StudioEclipse PDT视而不见
  • 3. 从未用过任何形式的版本控制系统,如Subclipse(AmirFish注:我用SVN
  • 4. 不采用某种编码与命名标准,以及通用约定,不能在项目开发周期里贯彻落实(AmirFish注:这部分一直在做,但是觉得还不够好!
  • 5. 不使用统一开发方式(AmirFish注:不太明白)
  • 6. 不转换(或)也不验证某些输入或SQL查询串(译注:参考PHP相关函数
  • 7. 不在编码之前彻底规划你的程序(AmirFish注:或许还不够彻底,没有深入每一个细节)
  • 8. 不使用测试驱动开发
  • 9. 不在错误开启状态下进行编码和测试(译注:参考PHP函数error_reporting
  • 10. 对调试器的好处视而不见(AmirFish注:我用Xdebug
  • Continue reading ‘40个迹象表明你还是PHP菜鸟’

    Tags:

Apache DefaultCharSet导致网页乱码

最近做的一个项目,把demo传到客户服务器上,有些模块变成了乱码,设置所有的JS运行都出了问题。而在我本机环境、手上另外一台服务器上测试均无问题。

那么,先明确导致乱码的核心问题:编码不一致!
呃,如果是个人兴趣问题,打了脑残体之类的文字,则不在本文讨论范围。

1、网页的meta信息。例如:

<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />

2、文件保存时的编码选择。例如:

3、程序通过header()函数指定编码。例如php:

header(”Content-Type:text/html; charset=utf-8″);

4、客户端浏览器的编码选择

5、服务器端的WEB服务器编码设置,如Apache、IIS。

上述4个地方,有任何一个编码不一致,都可能导致乱码。例如网页的meta你设置的是utf-8,而保存时就选择gbk编码。再或者你网页是utf-8编码,你在浏览器里选择gbk编码查看,那么…可以尝试一下

今天我遇到的问题呢?首先可以确保的是meta和文件保存时的编码是一致的,均为utf-8。但当我用浏览器访问出问题的网页时,浏览器自动选择了gbk编码,乱码!当我把浏览器编码切换到utf-8编码时,网页正常。

抓取网页的header信息。发现utf-8的网页被服务器发送回来竟然变成了gbk

如此看来一定是apache的设置问题咯。

打开apache的配置文件,httpd.conf。

查找:defaultcharst

果然有这么一行  AddDefaultCharset GB2312。

注释掉,保存,重启apache。

一切恢复正常。

小结:

1、如果 apache 设置了 defaultcharset ,并且程序里没有使用header()指定编码。那么最终输出以apache的AddDefaultCharse为主。

2、除非特别必要,否者不要设置apache的AddDefaultCharse。

Tags: , ,