preg_split () проблема со строками, содержащими '&' - PullRequest
1 голос
/ 23 апреля 2011

Я использую 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.';

, тогда после '&' прекращается сопоставление.

Ответы [ 2 ]

1 голос
/ 23 апреля 2011

Ваш 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] => .
)
0 голосов
/ 23 апреля 2011

Ваш скрипт:

<code>$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).'
';

Мой вывод:

Array
(
    [0] => this is test
    [1] => .
    [2] =>  we are testing this & we are over
    [3] => .
)

Я не понимаю вашу проблему.

...