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

前端 未结 7 1158
刺人心
刺人心 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条回答
  • 2021-02-14 02:57

    I'd probably use any of the multitudes of substring/string-split functions in PHP (some mentioned here already). But also look for ". " OR ".\n" (and possibly ".\n\r") instead of just ".". Just in case for whatever reason, the sentence contains a period that isn't followed by a space. I think it will harden the likelihood of you getting genuine results.

    Example, searching for just "." on:

    "I like stackoverflow.com."
    

    Will get you:

    "I like stackoverflow."
    

    When really, I'm sure you'd prefer:

    "I like stackoverflow.com."
    

    And once you have that basic search, you'll probably come across one or two occasions where it may miss something. Tune as you run with it!

    0 讨论(0)
提交回复
热议问题