如何将&字符转换为HTML字符?

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

should be:

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

How do I do that in PHP?

You need one of these:

html_entity_decode()
htmlspecialchars_decode()

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

Are you looking for html_entity_decode?

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!"; ?>