Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将htmlentities应用于剥离的标记_Php_Regex_Html Entities_Strip Tags - Fatal编程技术网

Php 将htmlentities应用于剥离的标记

Php 将htmlentities应用于剥离的标记,php,regex,html-entities,strip-tags,Php,Regex,Html Entities,Strip Tags,研究链接: 和 他们很接近,但不像预期的那样 我试过什么? <?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;",

研究链接:

他们很接近,但不像预期的那样

我试过什么?

<?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>");
?>

此函数可能会对您有所帮助,但未经过高度测试。它将对所有标记(指定的标记除外)执行htmlentities

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;
}
函数html\u实体\u解码\u匹配($matches){
返回html_实体_解码($matches[0]);
}
函数htmlentities_exclude($string,$exclude_数组){
$string=htmlentities($string);//htmlentities所有
$ent_sl=“”;/>
if(is_数组($exclude_数组)和!empty($exclude_数组)){
foreach($exclude_数组为$exc){
$exc=str_替换(数组(“”,“,$exc);

$ent=str\u replace(“/”,“\/”,htmlentities(您可以使用PHP DOM对象来实现这一点,首先创建一个元素(在您的示例中是),并提供编码字符串作为其主体(内部HTML),如下所示

    <?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;
    ?>

您可以使用内置函数,而不是创建这样的函数

<?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;
?>


如果第二个链接包含您可以尝试调整的解决方案,您列出“已研究链接”的原因是什么?@MarcinOrlowski再次阅读我的问题,您可能知道:)@deceze谢谢。我可能会寻找DOMDocument…很好的答案..不过这更像是净化用户输入…但是如果标签用于研究目的呢?strip_标签将完全删除它…创建元素很棘手。无论如何,感谢您指向该方向…+1
echo htmlentities_exclude('<b><script>alert("something");</script></b>', array("<b>"));

Output:
<b>&lt;script&gt;alert(&quot;something&quot;);&lt;/script&gt;</b>
    <?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;
    ?>
<?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;
?>