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 に答える