php PHP中的HTML编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1873793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:07:57  来源:igfitidea点击:

Html encode in PHP

phphtml-encode

提问by Mathias F

What is the easiest way to Html encode in PHP?

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

回答by Vallières

By encode, do you mean: Convert all applicable characters to HTML entities?

通过编码,您的意思是:将所有适用的字符转换为 HTML 实体?

htmlspecialcharsor htmlentities

htmlspecialchars或者 htmlentities

You can also use strip_tags if you want to remove all HTML tags :

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

strip_tags

strip_tags

Note: this will NOT stop all XSS attacks

注意:这不会阻止所有XSS 攻击

回答by Akhila Prakash

Encode.php

编码.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

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> 

You can encode HTML like this .

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

回答by Moby M

Try this:

尝试这个:

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

回答by Lu Blue

I searched for hours, and I tried almost everything suggested.
This worked for almost every entity :

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

$input = "ā???ū?rū?ī? ○ àéò ??? ? ?? ? ?? ↙ ??";


echo htmlentities($input, ENT_HTML5  , 'UTF-8');

result :

结果 :

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