Does anyone have a PHP snippet of code for grabbing the first “sentence” in a string?

前端 未结 7 1164
刺人心
刺人心 2021-02-14 02:28

If I have a description like:

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

7条回答
  •  闹比i
    闹比i (楼主)
    2021-02-14 02:49

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

    Find termination characters followed by a space

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

提交回复
热议问题