PHP6的新特性:Unicode和TextIterator – 馬永占 譯

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.