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

前端 未结 10 2083
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:43

    Use PHP's DOMDocument class to normalize an HTML fragment:

    $dom= new DOMDocument();
    $dom->loadHTML('

    Hello World'); $xpath = new DOMXPath($dom); $body = $xpath->query('/html/body'); echo($dom->saveXml($body->item(0)));

    This question is similar to an earlier question and I've copied and pasted one solution here. If the HTML is submitted by users you'll also need to filter out potential Javascript attack vectors like onmouseover="do_something_evil()" or .... Tools like HTML Purifier were designed to catch and solve these problems and are far more comprehensive than any code that I could post.

提交回复
热议问题