Using PHP substr() and strip_tags() while retaining formatting and without breaking HTML

前端 未结 10 2114
Happy的楠姐
Happy的楠姐 2020-11-27 16:44

I have various HTML strings to cut to 100 characters (of the stripped content, not the original) without stripping tags and without breaking HTML.

Original H

10条回答
  •  醉话见心
    2020-11-27 17:28

    try this function

    // trim the string function
    function trim_word($text, $length, $startPoint=0, $allowedTags=""){
        $text = html_entity_decode(htmlspecialchars_decode($text));
        $text = strip_tags($text, $allowedTags);
        return $text = substr($text, $startPoint, $length);
    }
    

    and

    echo trim_word("

    abcasdsdasasdas

    ","6");

提交回复
热议问题