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

前端 未结 27 1167
猫巷女王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 08:13

    I believe this is the easiest way to do it:

    $lines = explode('♦♣♠',wordwrap($string, $length, '♦♣♠'));
    $newstring = $lines[0] . ' • • •';
    

    I'm using the special characters to split the text and cut it.

提交回复
热议问题