现在的位置: 首页 > 综合 > 正文

PHP6的新特性:Unicode和TextIterator – 马永占 译

2013年09月02日 ⁄ 综合 ⁄ 共 2786字 ⁄ 字号 评论关闭

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。http://blog.csdn.net/mayongzhan - 马永占,myz,mayongzhan

PHP6的新特性:Unicode和TextIterator
原文地址:http://blog.makemepulse.com/2008/03/13/php6-unicode-and-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 } unicode(1) “!” { 0021 } unicode(1) ” ” { 0020 }

分解得到的是单词,为什么在一个单词后面的{}里面是很多的编码呢?我们来做个实验:

echo " / u0046 / u0072 / u0061 / u006e / u00e7 / u0061 / u0069 / u0073 " ;

我们得到这样的结果:“Français”。
PHP6可以对字母或者单词做处理!

$sentences = "Bonjour, nous sommes Français";
$word_break = new TextIterator($sentences, TextIterator::WORD);

取最后一个单词:

$word_break->preceding($word_break->last());
echo $word_break->current();

取第一个单词:

$word_break->first();
echo $word_break->current();

取第三个单词:
$word_break->first();
$word_break->next(3);
echo $word_break->current();

这仅仅是PHP6关于Unicode中的一部分,接下来我要测试在去参加巴黎的PHP会议时学到的
“str_transliterate”,这个str_transliterate可以实现对单词的不同语言的音译。

$name = "Antoine Ughetto";
$jap = str_transliterate($name, 'Latin', 'Katakana');
echo  str_transliterate($jap, 'Any', 'Latin');

噢,耶,我的名字是日语(アントイネウグヘット)听起来像是"antoine uguhetto " 。

所有这一切都非常有趣,只是没有手册这些测试起来很困难。
感谢Andrei Zmievski的博客文章帮助我做了这些测试。。。

 

 

PHP6, Unicode and TextIterator features - Antoine Ughetto

I’ve just install the last version of PHP6 dev and I’ve decided to test the famous new feature, the PHP Unicode Support. I will not explain new things about PHP6 or Unicode or TextIterator, it’s just my discoveries test on this features.
So the first thing to do is to enable PHP6 Unicode in the php.ini file.
;;;;;;;;;;;;;;;;;;;;
; 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
(more…)

 

抱歉!评论已关闭.