从PHP中的变量返回第一句话 [英] Returning first sentence from variable in PHP

查看:38
本文介绍了从PHP中的变量返回第一句话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了一个类似的帖子:

I have found a similar thread already where I got:

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

这在我的函数中似乎不起作用:

This doesn't seem to work in my function though:

<?function first_sentence($content) {
    $content = html_entity_decode(strip_tags($content));   
    $content = preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', $content);
    return $content;


}?>

当一个句子作为一个段落结束时,它似乎没有考虑到第一句.有什么想法吗?

It seems to be not taking into account the first sentence when a sentence ends as the end of a paragraph. Any ideas?

推荐答案

这是正确的语法,抱歉我的第一反应是:

Here's correct syntax, sorry for my first response:

function firstSentence($string) {
      $sentences = explode(".", $string);
      return $sentences[0];
}

这篇关于从PHP中的变量返回第一句话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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