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 -----
A Pescara, 1.500 sfollati per l'esondazione del Fosso Vallelunga.
----- next -----
年初以来,农业损失已达十亿欧元。
----- next -----
To Top