How to Truncate a string in PHP to the word closest to a certain number of characters?

前端 未结 27 1510
猫巷女王i
猫巷女王i 2020-11-22 07:28

I have a code snippet written in PHP that pulls a block of text from a database and sends it out to a widget on a webpage. The original block of text can be a lengthy artic

27条回答
  •  爱一瞬间的悲伤
    2020-11-22 07:56

    This is how i did it:

    $string = "I appreciate your service & idea to provide the branded toys at a fair rent price. This is really a wonderful to watch the kid not just playing with variety of toys but learning faster compare to the other kids who are not using the BooksandBeyond service. We wish you all the best";
    
    print_r(substr($string, 0, strpos(wordwrap($string, 250), "\n")));
    

提交回复
热议问题