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.0.0 IntlBreakIterator 现在实现了 IteratorAggregate。之前实现了 Traversable

目录

添加注释

用户贡献的注释 1 则注释

SenseException
10 年前
由于目前还没有关于 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 -----
Dall'inizio dell'anno l'agricoltura ha subito un miliardo di euro di danni.
----- next -----
To Top