函数名:IntlRuleBasedBreakIterator::__construct()
适用版本:PHP 7.0.0 及以上版本
用法:IntlRuleBasedBreakIterator::__construct() 函数用于创建一个新的 IntlRuleBasedBreakIterator 对象。
参数:
返回值:返回一个新的 IntlRuleBasedBreakIterator 对象。
示例:
$rules = <<<RULES
!forward;
\p{Z} & !\p{Zs} & !\p{Zl} & !\p{Zp} {rule_break: true};
RULES;
$breakIterator = new IntlRuleBasedBreakIterator($rules, IntlRuleBasedBreakIterator::BREAK_TYPE_SENTENCE);
$text = "This is a sample sentence. It demonstrates the usage of IntlRuleBasedBreakIterator.";
$breakIterator->setText($text);
foreach ($breakIterator as $boundary) {
echo substr($text, $boundary[0], $boundary[1] - $boundary[0]) . PHP_EOL;
}
在上面的示例中,我们首先定义了一个包含断句规则的字符串。然后,我们使用 IntlRuleBasedBreakIterator::__construct() 函数创建了一个 IntlRuleBasedBreakIterator 对象,并指定断句类型为 IntlRuleBasedBreakIterator::BREAK_TYPE_SENTENCE。接下来,我们使用 setText() 函数设置要断句的文本。最后,我们使用 foreach 循环遍历断句边界,并打印出每个断句的内容。输出结果如下:
This is a sample sentence.
It demonstrates the usage of IntlRuleBasedBreakIterator.