gpt4 book ai didi

php - 有没有人有一段 PHP 代码来获取字符串中的第一个 "sentence"?

转载 作者:可可西里 更新时间:2023-10-31 22:53:53 26 4
gpt4 key购买 nike

如果我有这样的描述:

"We prefer questions that can be answered, not just discussed. Provide details. Write clearly and simply."

我想要的是:

"We prefer questions that can be answered, not just discussed."

我想我会搜索一个正则表达式,比如“[.!\?]”,确定 strpos,然后从主字符串做一个 substr,但我想这是一个常见的事情,所以希望有人有一个片段躺在身边。

最佳答案

一个稍微昂贵的表达式,但是如果你想选择多种类型的标点符号作为句子终止符,它会更适应。

$sentence = preg_replace('/([^?!.]*.).*/', '\\1', $string);

查找后跟空格的终止字符

$sentence = preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', $string);

关于php - 有没有人有一段 PHP 代码来获取字符串中的第一个 "sentence"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1135467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com