著作權聲明:原創作品,允許轉載,轉載時請務必以超連結形式標明文章原始出版、作者資訊和本聲明。否則將追究法律責任。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…)