PHP Conference Japan 2024

IntlBreakIterator 類別

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

簡介

「斷詞迭代器」是一個 ICU 物件,它公開了用於在文字中定位邊界的方法(例如,單詞或句子邊界)。PHP IntlBreakIterator 作為所有類型 ICU 斷詞迭代器的基底類別。在提供額外功能的地方,intl 擴充功能可能會使用合適的子類別公開 ICU 斷詞迭代器,例如 IntlRuleBasedBreakIteratorIntlCodePointBreakIterator

此類別實作 IteratorAggregate。遍歷 IntlBreakIterator 會產生非負整數值,表示文字邊界的連續位置,表示為 UTF-8 程式碼單元(位元組)計數,從文字開頭計算(位置為 0)。迭代器產生的鍵僅形成自然數的序列 {0, 1, 2, …}

類別概要

class IntlBreakIterator implements IteratorAggregate {
/* 常數 */
public const int DONE;
public const int WORD_NONE;
public const int WORD_NONE_LIMIT;
public const int WORD_NUMBER;
public const int WORD_NUMBER_LIMIT;
public const int WORD_LETTER;
public const int WORD_LETTER_LIMIT;
public const int WORD_KANA;
public const int WORD_KANA_LIMIT;
public const int WORD_IDEO;
public const int WORD_IDEO_LIMIT;
public const int LINE_SOFT;
public const int LINE_SOFT_LIMIT;
public const int LINE_HARD;
public const int LINE_HARD_LIMIT;
public const int SENTENCE_TERM;
public const int SENTENCE_TERM_LIMIT;
public const int SENTENCE_SEP;
public const int SENTENCE_SEP_LIMIT;
/* 方法 */
private __construct()
public current(): int
public first(): int
public following(int $offset): int
public getErrorCode(): int
public getLocale(int $type): string|false
public getPartsIterator(string $type = IntlPartsIterator::KEY_SEQUENTIAL): IntlPartsIterator
public getText(): ?string
public isBoundary(int $offset): bool
public last(): int
public next(?int $offset = null): int
public preceding(int $offset): int
public previous(): int
public setText(string $text): bool
}

更新日誌

版本 描述
8.4.0 類別常數現在已輸入型別。
8.0.0 IntlBreakIterator 現在實作了 IteratorAggregate。先前,實作的是 Traversable

目錄

新增註解

使用者提供的註解 1 則註解

SenseException
11 年前
由於目前還沒有 IntlBreakIterator 用法的範例,我做了一個小的範例

<?php

$text
= "Si contano i danni. Un morto a Roma, un treno ".
"deragliato e quattro feriti a Foggia, strade chiuse in tutto ".
"il sud, allagamenti e danni sulla costa ionica. A Pescara, ".
"1.500 sfollati per l'esondazione del Fosso Vallelunga. ".
"Dall'inizio dell'anno l'agricoltura ha subito un miliardo ".
"di euro di danni.";

$locale = 'it_IT';

$i = IntlBreakIterator::createSentenceInstance($locale);
$i->setText($text);

foreach(
$i->getPartsIterator() as $sentence) {
echo
$sentence . PHP_EOL . '----- next -----' . PHP_EOL;
}

?>

結果

Si contano i danni.
----- next -----
Un morto a Roma, un treno deragliato e quattro feriti a Foggia, strade chiuse in tutto il sud, allagamenti e danni sulla costa ionica.
----- next -----
在佩斯卡拉,因瓦倫加溝渠氾濫,1500人流離失所。
----- next -----
自年初以來,農業已遭受十億歐元的損失。
----- next -----
To Top