函式名:IntlBreakIterator::setText()
函式功能:設定要分析的文字
函式簽名:public function setText(string $text): void
引數說明:
- $text:要分析的文字字串。
返回值:無
函式用法:
- 建立一個IntlBreakIterator物件:
$iterator = new IntlBreakIterator('en_US', IntlBreakIterator::WORD);
- 使用setText()方法設定要分析的文字:
$text = "Hello, World! This is a sample text.";
$iterator->setText($text);
函式示例:
$iterator = new IntlBreakIterator('en_US', IntlBreakIterator::WORD);
$text = "Hello, World! This is a sample text.";
// 設定要分析的文字
$iterator->setText($text);
// 遍歷分析結果
foreach ($iterator as $key => $value) {
echo "[$key] => " . mb_substr($text, $value, $iterator->getRuleStatus()) . "\n";
}
輸出結果:
[0] => Hello
[6] => ,
[8] =>
[9] => World
[14] => !
[15] =>
[16] => This
[21] =>
[22] => is
[25] =>
[26] => a
[28] =>
[29] => sample
[36] =>
[37] => text
[41] => .
注意事項:
- 在使用setText()方法之前,必須先建立一個IntlBreakIterator物件。
- setText()方法只接受一個字串作為引數,如果傳入的引數不是一個有效的字串,會丟擲一個異常。
- setText()方法會重置已有的分析結果,所以如果需要分析多個文字,需要在每次呼叫setText()之前建立新的IntlBreakIterator物件。