Shorten text without splitting words or breaking html tags

前端 未结 8 1351
粉色の甜心
粉色の甜心 2020-12-28 17:06

I am trying to cut off text after 236 chars without cutting words in half and preserving html tags. This is what I am using right now:

$shortdesc = $_helper-         


        
8条回答
  •  执笔经年
    2020-12-28 17:31

    Can I just give a thought ?

    Sample text :

    Lorem ipsum dolor sit amet, magna aliquyam erat, duo dolores et ea rebum. Stet clita kasd gubergren hello
    

    First, parse it into:

    array(
        '0' => array(
            'tag' => '',
            'text' => 'Lorem ipsum dolor sit amet, '
        ),
        '1' => array(
            'tag' => '',
            'text' => 'magna aliquyam erat',
        )
        '2' => ......
        '3' => ......
    )
    

    then cut the text one by one, and wrap each one with its tag after cut,

    then join them.

提交回复
热议问题