2016-05-09 53 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標籤