preg_split() 包含 '&' 的字符串问题

发布于 2024-11-02 15:55:11 字数 328 浏览 1 评论 0原文

我正在使用 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.';

那么它会在“&”之后停止匹配。

I am using preg_split() to get array of sentence from a string.

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

But when $text contains '&', for example:

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

then it stops matching after the '&'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亣腦蒛氧 2024-11-09 15:55:11

您的 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] => .
)

Your preg_split handles sentences with ampersands correctly, for example:

$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);

Output:

Array
(
    [0] => Sample sentence
    [1] => .
    [2] =>  Another sentence
    [3] => !
    [4] =>  Sentence with the special character & (ampersand)
    [5] => .
    [6] =>  Last sentence
    [7] => .
)
玩物 2024-11-09 15:55:11

你的脚本:

$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>';

我的输出:

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

我不明白你的问题。

Your Script:

$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>';

My Output:

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

I don't understand your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文