有没有人有一个PHP代码段来抓取第一个“句子”?一串? [英] Does anyone have a PHP snippet of code for grabbing the first "sentence" in a string?

查看:76
本文介绍了有没有人有一个PHP代码段来抓取第一个“句子”?一串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的描述:


我们更喜欢可以回答的问题,而不仅仅是讨论的问题。请提供详细信息。请写清楚而且简单。

"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,但是我想这是很平常的事情,所以希望有人在附近放一个片段。

I figure I would search for a regular expression, like "[.!\?]", determine the strpos and then do a substr from the main string, but I imagine it's a common thing to do, so hoping someone has a snippet lying around.

推荐答案

稍微昂贵一点的表达式,但是,如果您希望选择多种类型的标点符号作为句子终止符,则该表达式将更具适应性。

A slightly more costly expression, however will be more adaptable if you wish to select multiple types of punctuation as sentence terminators.

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

查找终止字符后跟空格

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

这篇关于有没有人有一个PHP代码段来抓取第一个“句子”?一串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆