【问题标题】:PHP function to remove the first sentence from a paragraphPHP函数从段落中删除第一句话
【发布时间】:2021-04-05 11:01:11
【问题描述】:

我发现从段落中返回第一句话的功能非常有用。

参考:Returning first sentence from variable in PHP

我在 xml 文件中有产品描述,我想获取标题的第一句话,其余的作为描述。

例子:

Lorem ipsum dolor sit amet。 Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua。 Ut enim ad minim veniam。

通过推荐,我得到标题:Lorem ipsum dolor sit amet。

我想得到描述:Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua。 Ut enim ad minim veniam。

从段落中删除第一句话的功能是什么? (句子变体 end = '.?!')

【问题讨论】:

    标签: php


    【解决方案1】:

    你可以使用explode函数

    $paragraph = 'Lorem ipsum dolor sit amet. Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam';
    
    return explode('.', $paragraph)[1];  //here i want second index
    

    这个函数返回一个数组,所以要获取特定的句子指定句子的索引

    【讨论】:

    • 谢谢,但我不会写函数,请你帮忙?
    • 我找到了一个函数,但我不知道如何包含比“.”更多的符号,我的描述以“.!”结尾
    • 函数 last_sentence($content) { $pos = strrpos($content, '.'); $pos_two = strrpos($content, '.', $pos - strlen($content) - 1); if($pos_two === false) { 返回 $content; } else { return substr($content, $pos_two+1); } }
    • 所以如果你想用多个分隔符分割字符串你可以使用php.net/manual/en/function.preg-split.php函数你也可以看到这个stackoverflow.com/questions/4955433/…
    猜你喜欢
    • 2014-11-10
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    相关资源
    最近更新 更多