How to convert & characters to HTML characters?

江枫思渺然 提交于 2019-12-22 10:25:10

问题


<?php echo "Hello World!"; ?>

should be:

<?php echo "Hello World!"; ?>

How do I do that in PHP?


回答1:


You need one of these:

html_entity_decode()
htmlspecialchars_decode()
  • html_entity_decode() in PHP Manual
  • htmlspecialchars_decode() in PHP Manual

The main difference is that html_entity_decode() will translate all the HTML entities in your string (&lt; becomes <, &aacute; becomes á, etc.) while html_specialchars_decode() only translates some special HTML entities:

The converted entities are: &amp;, &quot; (when ENT_NOQUOTES is not set), &#039; (when ENT_QUOTES is set), &lt; and &gt;.




回答2:


htmlspecialchars_decode()




回答3:


Are you looking for html_entity_decode?




回答4:


If you're actually trying to do this manually, instead of with html_entity_decode, try str_replace.

$needle = array("&lt;","&gt;");
$replace = array("<", ">");
$string = '&lt;?php echo "Hello World!"; ?&gt;';

$string = str_replace($needle, $replace, $string);

print $string; // prints <?php echo "Hello World!"; ?>


来源:https://stackoverflow.com/questions/6665985/how-to-convert-characters-to-html-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!