【问题标题】:Html encode in PHPPHP中的HTML编码
【发布时间】:2010-12-24 19:59:12
【问题描述】:

在 PHP 中进行 Html 编码最简单的方法是什么?

【问题讨论】:

  • 我认为他的意思是php相当于ASP.NET方法“htmlencode”。它用于通过将 '

标签: php html-encode


【解决方案1】:

编码是指:将所有适用的字符转换为 HTML 实体?

htmlspecialcharshtmlentities

如果你想删除所有 HTML 标签,你也可以使用 strip_tags :

strip_tags

注意:这不会停止所有XSS attacks

【讨论】:

  • 我不知道我必须使用哪个。我需要这个来避免 XSS 攻击。
  • 那么 htmlspecialchars 应该可以解决问题。或者将 filter_var 与 FILTER_SANITIZE_SPECIAL_CHARS 过滤器一起使用。
  • htmlspecialchars > htmlentities 在大多数情况下。非 ASCII 字符的 HTML 实体应该成为过去;只需使用 UTF-8 并将字符直接放入。
  • php 中的 htmlspecialchars 优先
【解决方案2】:

编码.php

<h1>Encode HTML CODE</h1>

<form action='htmlencodeoutput.php' method='post'>
<textarea rows='30' cols='100'name='inputval'></textarea>
<input type='submit'>
</form>

htmlencodeoutput.php

<?php

$code=bin2hex($_POST['inputval']); 
$spilt=chunk_split($code,2,"%");
$totallen=strlen($spilt);
 $sublen=$totallen-1;
 $fianlop=substr($spilt,'0', $sublen);
$output="<script>
document.write(unescape('%$fianlop'));
</script>";

?> 
<textarea rows='20' cols='100'><?php echo $output?> </textarea> 

您可以像这样对 HTML 进行编码。

【讨论】:

    【解决方案3】:

    试试这个:

    <?php
        $str = "This is some <b>bold</b> text.";
        echo htmlspecialchars($str);
    ?>
    

    【讨论】:

    • 为什么要“试试这个”?这不是一个非常慷慨/内容丰富的答案。
    【解决方案4】:

    我搜索了几个小时,几乎尝试了所有建议。
    这几乎适用于所有实体:

    $input = "āžšķūņrūķīš ○ àéò ∀∂∋ ©€ ♣♦ ↠ ↔↛ ↙ ℜ℞";
    
    
    echo htmlentities($input, ENT_HTML5  , 'UTF-8');
    

    结果:

    &amacr;&zcaron;&scaron;&kcedil;&umacr;&ncedil;r&umacr;&kcedil;&imacr;&scaron; &cir; &agrave;&eacute;&ograve; &forall;&part;&ReverseElement; &copy;&euro; &clubs;&diamondsuit; &twoheadrightarrow; &harr;&nrarr; &swarr; &Rfr;&rx;rx;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多