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

支持中文的截取字符函数,不同编码下中文字符的范围一目了然

这是一个简单的函数,或许对你我都有用吧。
以前也有类似的文章,这个我仍然和上次一样,用来记录这些正则,无它。。。
原文作者阿彪,来自:http://www.bloomandkevin.cn/2009/02/blog-post.html

PHP代码
  1. <?php  
  2. /* 
  3. * 中文截取,支持gb2312,gbk,utf-8,big5 
  4. * 
  5. * @param string $str 要截取的字串 
  6. * @param int $start 截取起始位置 
  7. * @param int $length 截取长度 
  8. * @param string $charset utf-8|gb2312|gbk|big5 编码 
  9. * @param $suffix 是否加尾缀 
  10. */  
  11. public function csubstr($str$start=0, $length$charset="utf-8"$suffix=true)  
  12. {  
  13.     if(function_exists("mb_substr"))  
  14.         return mb_substr($str$start$length$charset);  
  15.   
  16.     $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";  
  17.     $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";  
  18.     $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";  
  19.     $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";  
  20.   
  21.     preg_match_all($re[$charset], $str$match);  
  22.     $slice = join("",array_slice($match[0], $start$length));  
  23.     if($suffixreturn $slice."…";  
  24.      
  25.     return $slice;  
  26. }  
  27. ?>  

Tags: 中文截取

PHP生成条形码 之一 条形码介绍

条形码这个东西,在实际中用的比较多,而且现在大多数的手机(带摄像头的)几乎都支持条形码拍摄识别了。

曾经想过为老头子的小店搞一个这样的程序,方便他统计货物,但后来想想,这么小的小店,数都数的过来就放弃了。其实还有一个原因,那就是,手持PDA太贵 了。

这是条形码的资料,先学习一下,说不定哪天老头子的小店能够做大呢?HOHO

内容如下:我是从博客园COPY而来的:http://www.cnblogs.com/gnielee/archive/2009/07/29/1533768.html

     在一些生产制造行业条形码是必不可少的。比如,打印工作单、原材料出入库、成品出库发货等都需要扫描条形码进行数据采集。最近发现公司打印出的工作单上出 现了条码,打印工单的程序是用PHP写的,于是乎看看源代码是怎么实现的。原来是通过一个生成Barcode的类将相应的数据转给该程序并生成JPG文 件。
     自己在网上搜了搜实现Barcode的其他方法,找到一个国外的开源程序专门用来生成“一维条形码”。首先,复习一下条形码的相关知识。

条形码分类

一、按码制分类
1. UPC码
1973年,美国率先在国内的商业系统中应用于UPC码之后加拿大也在商业系统中采用UPC码。 UPC码是一种长度固定的连续型数字式码制,其字符集为数字0~9。它采用四种元素宽度,每个条或空是1、2、3或4倍单位元素宽度。IPC码有两种类 型,即UPC-A码和UPC-E码。

2. EAN码
1977年,欧洲经济共同体各国按照UPC码的标准制定了欧洲物品编码EAN 码,与UPC码兼容,而且两者具有相同的符号体系。EAN码的字符编号结构与UPC码相同,也是长度固定的、连续型的数字式码制,其字符集是数字0~9。 它采用四种元素宽度,每个条或空是1、2、3或4倍单位元素宽度。EAN码有两种类型,即EAN-13码和EAN-8码。

3. 交叉25码
交叉25码是一种长度可变的连续型自校验数字式码制,其字符集为数字0~9。采用两种元素宽度,每个条和空是宽或窄元素。编码字符个数为偶数,所有奇数位置上的数据以条编码,偶数位置上的数据以空编码。如果为奇数个数据编码,则在数据前补一位0,以使数据为偶数个数位。

4. 39码
39 码是第一个字母数字式码制。1974年由Intermec公司推出。它是长度可比的离散型自校险字母数字式码制。其字符集为数字0—9,26个大写字母和 7特殊字符(-、。、Space、/、%、¥),共43个字符。每个字符由9个元素组成,其中有5个条(2个宽条,3个窄条)和4个空(1个宽空,3个窄 空),是一种离散码。

5. 库德巴码
库德巴码(Code Bar)出现于1972年,是一种长度可变的连续型自校验数字式码制。其字符集为数字0—9和6个特殊字符(-、:、/、。、+、¥),共16个字符。常用于仓库、血库和航空快递包裹中。

6. 128码
128 码出现于1981年,是一种长度可变的连续型自校验数字式码制。它采用四种元素宽度,每个字符由3个条和3个空,共11个单元元素宽度, 又称(11,3)码。它由106个不,同条形码字符,每个条形码字符有三种含义不同的字符集,分别为A、B、C。它使用这3个交替的字符集可将128个 ASCII码编码。

7. 93码
93码是一种长度可变的连续型字母数字式码制。其字符集成为数字。0-9,26个大写字母和7个特殊字符(-、。、Space、/、+、%、¥)以及4个控制字符。每个字符由3个条和3个罕,共9个元素宽度。

8. 49码
49 码是一种多行的连续型、长度可变的字母数字式码制。出现于1987年,主要用于小物品标签上的符号。采用多种元素宽度。其字符集为数字0-9,26个大写 字母和7个特殊字符(-、。、Space、%、/、+、%、¥)、3个功能键(F1、 陀、F3)和3个变换字符,共49个字符。

9. 其他码制
除上述码外,还有其他的码制,例如25码出现于1977年,主要用于电子元器件标签;矩阵25码是11码的变形;Nixdorf码已被EAN码所取代Plessey码出现于1971年5月主要用于图书馆等。

二、按维数分类
1. 普通的一维条码
普通的一维条码自本问世以来,很快得到了普及并广泛应用。但是由于一维条码的信息容量很小,如 商品上的条码仅能容13位的阿拉伯数字,更多的描述商品的信息只能依赖数据库的支持,离开了预先建立的数据库,这种条码就变成了无源之水,无本之木,因而 条码的应用范围受到了一定的限制。

2. 二维条码
除具有普通条码的优点外,二维条码还具有信息容量大、可靠性高、保密防伪性强、 易于制作、成本低等优点。<BR>美国Symbol公司于1991年正式推出名为PDF417的二维条码,简称为PDF417条码,即“便携 式数据文件”。FDF417条码是一种高密度、高信息含量的便携式数据文件,是实现证件及卡片等大容量、高可靠性信息自动存储、携带并可用机器自动识读的 理想手段。

3. 多维条码
进入20世纪80年代以来,人们围绕如何提高条形码符号的信息密度,进行了研究工作。多维条形码和集装 箱条形码成为研究、以展与应用的方向。<BR>信息密度是描述条形码符号的一个重要参数据,即单位长度中可能编写的字母个数,通常记作:字母 个数/cm。影响信息密度的主要因素是条、空结构和窄元系的宽度。<BR>128码和93码就是人们为提高密度而进行的成功的尝试。128码 城1981年被推荐应用;而93码于1982年投入使用。这两种码的符号密度均比39码高将近30%。<BR>随着条形码技术的发展和条形码 三制的种类不断增加,条形码的标准化显得愈来愈重要。为此,曾先后制定了军用标准1189;交叉25码、39码和Coda Bar码ANSI标准MH10.8M等。同时,一些行业也开始建立行业标准,以适应发展的需要。此后,戴维·阿利尔又研制出49码。这是一种非传统的条形 码符号,它比以往的条形码符号具有更高的密度。特德·威廉姆斯(Ted Williams)GFI988推出16K码,该码的结构类似于49码,是一种比较新型的码制,适用于激光系统。

条形码实例

1. Codabar   Manual Reference

codabar

  • Supports: [0-9], [A-D], - $ : / . +
  • Must start and end with a letter
  • Contains an auto-checksum, no checksum number


2. Code 11   Manual Reference

code11

  • Supports: [0-9], -
  • Rarely used
  • Contains auto-checksum numbers


3. Code 39   Manual Reference

code39

  • Supports: [0-9], [A-Z], - . $ / + % [SPACE]
  • Really used, many uses
  • Contains a optional checksum
  • Resulting barcode is pretty big


4. Code 39 Extended   Manual Reference

code39ext

  • Supports: [0-9], [A-Z], - . $ / + % [SPACE] + ASCII 0-127
  • The extension of this barcode is optional. You must specify your reader that you read Code 39 Extended.


5. Code 93   Manual Reference

code93

  • Supports: [0-9], [A-Z], - . $ / + % [SPACE] + ASCII 0-127
  • Similar to Code 39 but less used
  • Supports ASCII 0-127 and is not optional like the Code 39
  • Bigger density


6. Code 128   Manual Reference

code128

  • Supports: [0-9], [a-zA-Z], tous les characteres
  • Contains 3 tables to better encode data
  • Groups numbers by two for a better compression


7. EAN-8   Manual Reference

ean8

  • Supports: [0-9]
  • Encodes 7 numbers and a checksum number
  • It is the EAN-13 short version
  • You have to get a number with a license to use in the public


8. EAN-13   Manual Reference

ean13

  • Supports: [0-9]
  • Encodes 12 numbers with a checksum number
  • It is the UPC-A extension which adds the country code
  • You have to get a number with a license to use in the public


9. ISBN-10 / ISBN-13   Manual Reference

isbn

  • Supports: [0-9]
  • Based on EAN-13, used for books
  • You have to get a number with a license to use in the public


10. Interleaved 2 of 5   Manual Reference

i25

  • Supports: [0-9]
  • Based on Standard 2 of 5
  • Optional checksum number


11. Standard 2 of 5   Manual Reference

s25

  • Supports: [0-9]
  • Hard to read for readers because really low density
  • Optional checksum number


12. MSI Plessey   Manual Reference

msi

  • Supports: [0-9]
  • Formerly used for inventory
  • Optional checksum number


13. UPC-A   Manual Reference

upca

  • Supports: [0-9]
  • Encoded like EAN-13
  • Encodes 11 numbers with a checksum number
  • The numbers depends on what you sell
  • You have to get a number with a license to use in the public


14. UPC-E   Manual Reference

upce

  • Supports: [0-9]
  • Short version of UPC-A
  • Encodes 7 numbers with a checksum number
  • The numbers depends on what you sell
  • You have to get a number with a license to use in the public


15. UPC Extension 2   Manual Reference

upcext2

  • Supports: [0-9]
  • Contains 2 characters
  • Indicates more information on the product
  • Must be joined to a UPC or EAN symbol


16. UPC Extension 5   Manual Reference

upcext5

  • Supports: [0-9]
  • Contains 5 characters
  • Usually indicates the suggested price of the product
  • Must be joined to a UPC or EAN symbol


17. PostNet   Manual Reference

postnet

  • Supports: [0-9]
  • Contains either 5, 9 or 11 numbers
  • Used to encode envelopes in United States


18. Other   Manual Reference

otherbarcode

  • Create your own barcode
  • Add the text you want


参考资料:http://www.barcodephp.com/1d/overview.php

Tags: 条形码

List of Supported Protocols/Wrappers

以下部分内容来自手册,比如这个列表:

对于这个列表,熟悉的人可能很熟悉,不熟悉的人,应该不太会关注,以前我也一样。
直到,我看到zend framework,发现在zend_view里,他就模拟了一个协议:zend.view://,为的就是将那些模版中采用短标签(<? =$a ?>)的代码改为长标签(<?php echo $a ; ?>)。

其实我再次看它,也只是一个顺便。yhustc想尝试把以下内容输出:

PHP代码
  1. <?php  
  2. $str = '今天是:<?php echo date("Y-m-d"); ?>';    
  3. file_put_contents('test.php'$str );  
  4. include'test.php' );   

输出结果是类似:今天是2009-01-01这样的结果,但事实上的输出却还是

XML/HTML代码
  1. 今天是:<?php echo date("Y-m-d"); ?>  

yhustc进行了尝试,如果这个内容写入文件,然后通过include,就可以显示正确的结果:

PHP代码
  1. <?php    
  2. $str = '今天是:<?php echo date("Y-m-d"); ?>';      
  3. file_put_contents('test.php'$str );    
  4. include'test.php' );  

但是yhustc不想要这个多出来的IO,因为写入文件后,你总还得删除吧?如果类似的东西太多,IO就太恐怖了。

于是我根据zend.view://这个协议,想起了PHP自带的。并使用Data的协议或者称Wrappers来解决了这个问题:

PHP代码
  1. $str = "函数使用 今天的日期: <?php echo date('Y-m-d');?>";  
  2. $strBase64 = base64_encode$str );  
  3. include("data://text/plain;base64,{$strBase64}") ;  

关于这个data的wrappers请查看上面的列表,我也就不详细说了,它是符合RRC2397的标准的。
事实上这个Data也是从5.2.0才开始支持的,而且更重要的是上面的第三行代码:

PHP代码
  1. include("data://text/plain;base64,{$strBase64}") ;   

这句话还需要另外一样东西支持:allow_url_include,而这个又需要allow_url_open的支持。(需要改php.ini)
有了这个支持,你以后看到这样的代码:

PHP代码
  1. <?php  
  2. include("http://localhost/needrequire.php");  
  3. echo 'test';  

就不需要更感到惊讶和愤怒了。毕竟,它事实上是支持的,只要这个needrequire.php是返回正确的PHP代码,比如test.php的内容为:

PHP代码
  1. <?php  
  2. echo'<?php class a{ function a(){echo "test";}}?>' );      
  3. ?>  

然后:

PHP代码
  1. <?php  
  2. include'http://localhost/test_echo.php' );  
  3. $a = new a();  

还是会正常的输出test的。

php 使用glob遍历时的注意事项[原创]

PHP中使用glob递归是一个很方便的事情,如下:

PHP代码
  1. function read ( $dirname )  
  2. {  
  3.     static $dInfo;  
  4.     $dirname .= subStr$dirname, -1 ) == "/"  ? "" : "/";  
  5.     $dirInfo = glob$dirname . "*" );  
  6.     foreach ( $dirInfo as $info ){  
  7.         $dInfo[] = $info;  
  8.         if ( is_dir$info ) ){  
  9.             if ( !is_readable$info ) ){  
  10.                 chmod$info, 0777 );  
  11.             }  
  12.             read( $info );  
  13.         }  
  14.     }  
  15.     return $dInfo;  
  16. }  
当然用dir函数也可以:

PHP代码
  1. function read ( $dirname )  
  2. {  
  3.     static $dInfo;  
  4.     $d = dir( $dirname );  
  5.     if ( $d ){  
  6.         while ( false !== ($entry = $d->read() ) ){  
  7.             if$entry != '.' && $entry != '..' ){  
  8.                 $entry = $dirname . '/' . $entry ;  
  9.                 $dInfo[] = $entry ;  
  10.                 ifis_dir$entry )){  
  11.                     if ( !is_readable$entry ) ){  
  12.                         chmod$entry, 0777 );  
  13.                     }  
  14.                     //$timeInfo[] = filemtime( $entry );  
  15.                     read( $entry );  
  16.                 }  
  17.             }  
  18.         }  
  19.     }  
  20.     return $dInfo;  
  21. }  
嗯。看上去没有什么问题。但是有一个情况需要注意

在linux下面,目录名可以为".aa",最典型的例子就是svn目录,svn目录里大量的".svn"的目录,好象,在linux下面,这样以"."开头的目录,默认是隐藏目录,不显示的。

所以,在使用glob的时候,这样的目录是认不出来的。这点要千万注意

做个笔记。

Tags: glob

What makes a good programmer?

本文来自nio's blog(nio?尼奥?黑客帝国?),原文如下:

What makes a good programmer?

Some casual surfing led me to this article from a couple of years ago, titled "How to recognize a good programmer". It was a nice read, but as many in the comments pointed out, the criteria the author set forth most likely describe himself and are not really useful as rules-of-thumb on how to recognize a good programmer.

It got me thinking though, on what are the attributes I consider useful in fellow programmers. So what makes a good programmer?

以下五项,按照优先级,你会怎样排序呢?

  • Security(安全性)
  • Maintainability(可维护性)
  • Usability(可用性)
  • Performance(性能)
  • LOC (lines-of-code) count(代码量)

作者认为最重要的是 usability,因为你开发的东西最终价值取决于最终用户。我们开发的目的是为了解决问题,如果解决不了问题,则说明项目是失败的。