您好,欢迎访问一九零五行业门户网

PHP6的新特性:Unicode和TextIterator

我刚刚安装了php6 dev版本,决定测试一下php6的新特性-php的unicode支持。我并没有打算讲php6的新特性或者是unicode,下面仅仅是我做的关于unicode的测试。
首先要做的是让php6支持unicode,在php.ini文件中修改。
;;;;;;;;;;;;;;;;;;;;
; unicode settings ;
;;;;;;;;;;;;;;;;;;;;unicode.semantics = on
unicode.runtime_encoding = utf-8
unicode.script_encoding = utf-8
unicode.output_encoding = utf-8
unicode.from_error_mode = u_invalid_substitute
unicode.from_error_subst_char = 3f
由于我使用的是法语和英语有所不同,有一些字符需要处理。
所以,我第一次试验的目的是检验strlen功能的unicode …
$word = être;
echo length: .strlen($word);
结果是: length: 4  。结果非常的正确… …但它仅仅是个开始! : )
我的第二个测试对象是与php6新的spl中的textiterator textiterator
$word = être;
foreach (new textiterator($word, textiterator::character) as $character) {
? var_inspect($character);
}
输出: unicode(1) “ê” { 00ea } unicode(1) “t” { 0074 } unicode(1) “r” { 0072 } unicode(1) “e” { 0065 }
分解单词,得到了很多的字母和字母的信息…
textiterator::character的操作看上去非常的强大啊,不过textiterator::word更强大
$sentences = bonjour, nous sommes français ! aïe :);
foreach (new textiterator($sentences, textiterator::word) as $word) {
    var_inspect($word);
}
得到的结果: unicode(7) “bonjour” { 0042 006f 006e 006a 006f 0075 0072 } unicode(1) “,” { 002c } unicode(1) ” ” { 0020 } unicode(4) “nous” { 006e 006f 0075 0073 } unicode(1) ” ” { 0020 } unicode(6) “sommes” { 0073 006f 006d 006d 0065 0073 } unicode(1) ” ” { 0020 } unicode(8) “français” { 0046 0072 0061 006e 00e7 0061 0069 0073 } unicode(1) ” ” { 0020 }
其它类似信息

推荐信息