2016-05-09 54 views
1

链接研究之:应用ヶ辆,以剥离标签

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

他们接近,但并不如预期。

我试过了什么?

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

我想实现什么?

如果输入的是:

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

那么输出将是:

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

有没有具体的黑名单,但有一个特定的白名单。

+1

如果第二个包含您可以尝试适应的解决方案,您列出“研究链接”的原因是什么? –

+0

http://stackoverflow.com/a/36840765/476 – deceze

+0

@MarcinOrlowski再次阅读我的问题,你可能知道:) – Karma

回答

2

此功能可能对您有所帮助,但未经过高度测试。它会针对不同的标签的所有标签ヶ辆指定

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> 
+0

我会调整它有点,但是啊....逻辑是不可否认的:)谢谢... +1 – Karma

1

您可以使用PHP DOM对象来实现这一点,首先创建一个元素(在你的情况下,它是< B> ),并提供编码的字符串作为其身体(内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; 
?> 
+0

好的答案..但它更像是消毒用户输入...但是如果

  • 11. 在Rails中剥离标签
  • 12. 剥离HTML标签 - TinyMCE的
  • 13. 防止HTML标签剥离
  • 14. PHP nodevalue剥离html标签
  • 15. jQuery HTMLCollection,标签剥离
  • 16. PHP DOMDocument剥离HTML标签
  • 17. PHP的XMLReader问题ヶ辆
  • 18. ヶ辆不产生laravel
  • 19. ヶ辆功能的JavaScript
  • 20. 一个createTextNode没有ヶ辆
  • 21. PHPヶ辆()未按预期
  • 22. 使用tinyMCE剥离的HTML标签
  • 23. 剥离HTML标签而不使用HtmlAgilityPack
  • 24. 重定向使用的htmlspecialchars /ヶ辆
  • 25. 误导 - ヶ辆不起作用
  • 26. 编码问题使用ヶ辆方法
  • 27. 用htmlspecialchars()或ヶ辆()不工作
  • 28. 使用Perl剥离除html标签以外的所有内容
  • 29. Ajax调用后的HTML响应被剥离了一些标签
  • 30. 剥离不完整的HTML标签