Apply htmlentities to stripped tags

只谈情不闲聊 提交于 2019-12-13 01:22:54

问题


Researched links:

How do you apply htmlentities selectively? and PHP function to strip tags, except a list of whitelisted tags and attributes

They are close but not as expected.

What have I tried?

<?php
define('CHARSET', 'UTF-8');
define('REPLACE_FLAGS', ENT_HTML5);

function htmlcleaned($string) {
    $string = htmlentities($string);
    return str_replace(
    array("&lt;i&gt;", "&lt;b&gt;", "&lt;/i&gt;", "&lt;/b&gt;", "&lt;p&gt;", "&lt;/p&gt;"),
    array("<i>", "<b>", "</i>", "</b>", "<p>", "</p>"), $string);
}

echo htmlcleaned("<p>How are you?</p><p><b>This is bold</b></p><p><i>This is italic</i></p><p><u>This is underline</u></p><p><br></p><ul><li>This is list item 1</li><li>This is list item 2</li></ul><p><br></p><ol><li>This is ordered list item 1</li><li>This is ordered list item 2</li></ol><p><a target='_blank' style='color: #1c5c76;' href='http://www.google.com'>http://www.google.com</a></p><p>This is plain text again.<br></p><script>alert('attempt csrf');</script><p><p>This is P tag example</p></p>");
?>

What I want to achieve?

if the input is:

<b><script>alert("something");</script></b>

then the output will be:

<b>&lt;script&rt;("something");&lt;/script$rt;</b>

There is no specific blacklist but there is a specific white list.


回答1:


This function might help you, it is not highly tested. It will do htmlentities on all the tags except the tags you specify

function html_entity_decode_matches($matches){
    return html_entity_decode($matches[0]); 
}
function htmlentities_exclude($string, $exclude_array){
    $string = htmlentities($string); //htmlentities all
    $ent_sl = "&gt;"; //>
    if (is_array($exclude_array) AND !empty($exclude_array)){
        foreach($exclude_array as $exc){
            $exc = str_replace(array("<", ">"), "", $exc);
            $ent = str_replace("/", "\/", htmlentities("<{$exc}"));
            $ent_e = str_replace("/", "\/", htmlentities("</{$exc}>"));
            //do decode on <tag...>
            $string = preg_replace_callback("/{$ent}(.*?){$ent_sl}/", "html_entity_decode_matches", $string);
            //do decode on <\tag>
            $string = preg_replace_callback("/{$ent_e}/", "html_entity_decode_matches", $string);
        }
    }
    return $string;
}

echo htmlentities_exclude('<b><script>alert("something");</script></b>', array("<b>"));

Output:
<b>&lt;script&gt;alert(&quot;something&quot;);&lt;/script&gt;</b>



回答2:


You can use PHP DOM objects to achieve this, first you create an element(In your case it is < b>) and provide encoded string as its body(inner HTML) like below,

    <?php
        define('CHARSET', 'UTF-8');
        define('REPLACE_FLAGS', ENT_HTML5);
        function htmlcleaned($string) {
            return str_replace(array("<", ">"), array("&lt;", "&gt;"), $string);
        }
        $dom = new DOMDocument('1.0', 'utf-8');
        $element = $dom->createElement('b', htmlcleaned('<script>alert("something");</script>'));
        $dom->appendChild($element);
        $html = $dom->saveXML();
        echo $html;
    ?>

You can use builtin function instead of creating a function like this,

<?php
    define('CHARSET', 'UTF-8');
    define('REPLACE_FLAGS', ENT_HTML5);
    $dom = new DOMDocument('1.0', 'utf-8');
    $element = $dom->createElement('b', htmlspecialchars('<script>alert("something");</script>', ENT_NOQUOTES));
    $dom->appendChild($element);
    $html = $dom->saveXML();
    echo $html;
?>


来源:https://stackoverflow.com/questions/37110629/apply-htmlentities-to-stripped-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!