簡體   English   中英

PHP的ent_quotes例外 <br> 和 <ar>

[英]PHP ent_quotes exception for <br> and <ar>

我試圖使用PHP將HTML隱蔽到實體,但是我需要除<br>和<a>標記之外。

這是我的代碼的示例

 <?php 
  $string[0] = "<a href='http://hidd3n.tk'>Needs to stay</a> Filler text in between 
 <br><br> <script src='http://malicious.com/'></script> NEEDS to go";
  $string[1] = htmlentities($string[0], ENT_QUOTES, "UTF-8");
 ?>

我建議您使用BBCode ,這樣會更安全。

編輯:

好吧,我已經想出了辦法。

使用此功能比上一個功能更安全:

function convert_myhtml_entities($string){
    $string = htmlentities($string, ENT_NOQUOTES, "UTF-8");
    $string = preg_replace('/&lt;\s*br\s*(\/|)\s*&gt;/U','<br$1>',$string);
    $string = preg_replace('/&lt;\s*a(.*)\s*&gt;/U','<a$1>',$string);
    $string = preg_replace('/&lt;\s*\/\s*a\s*&gt;/U','</a>',$string);
    return $string;
}

現在已使用上面的字符串進行了測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM