Wordwrap / Cut Text in HTML string

前端 未结 2 1548
我在风中等你
我在风中等你 2020-12-04 02:39

here what i want to do : i have a string containing HTML tags and i want to cut it using the wordwrap function excluding HTML tags.

I\'m stuck :

publ         


        
2条回答
  •  囚心锁ツ
    2020-12-04 03:25

    If usage of DOM parsing is not the aim and you need only trancate HTML — take a look at cot_string_truncate function in this Gist. It taken from Cotonti CMF.

    It's processed plain text or HTML as well. You can set length and choose how to trancate text — exact characters by limit or by word nearest boundary.

    It proper treats HTML entities and serial space characters as one (as it viewed in browser) — so your example should work well:

    $test_str = "

    Test to be cut

    Some text

    "; echo cot_string_truncate($test_str, 8);

    Result:

    Test to

提交回复
热议问题