1

我正在使用preg_split()从字符串中获取句子数组。

$sentences = preg_split("/([.?!\r\n]+)/", $text, 0, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);

但是当$text包含'&'时,例如:

$text = 'this is test. we are testing this & we are over.';

然后它在“&”之后停止匹配。

4

2 回答 2

1

您的 preg_split 正确处理带有 & 符号的句子,例如:

$text = 'Sample sentence. Another sentence! Sentence with the special character & (ampersand). Last sentence.';
$sentences = preg_split("/([.?!\r\n]+)/", $text, 0, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
print_r($sentences);

输出:

Array
(
    [0] => Sample sentence
    [1] => .
    [2] =>  Another sentence
    [3] => !
    [4] =>  Sentence with the special character & (ampersand)
    [5] => .
    [6] =>  Last sentence
    [7] => .
)
于 2011-04-22T21:10:25.313 回答
0

你的脚本:

$text = 'this is test. we are testing this & we are over.';
$sentences = preg_split("/([.?!\r\n]+)/", $text, 0, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
echo '<pre>'.print_r($sentences, true).'</pre>';

我的输出:

大批
(
    [0] => 这是测试
    [1] =>。
    [2] => 我们正在测试这个并且我们结束了
    [3] =>。
)

我不明白你的问题。

于 2011-04-22T21:10:20.877 回答