How can PHP code be highlighted and replaced within specific tags?

To highlight and replace PHP code within specific tags, you can use a combination of HTML `<pre>` tags for preserving whitespace and the `htmlspecialchars()` function to escape special characters in the PHP code. This will ensure that the PHP code is displayed correctly without being executed.

&lt;?php
$phpCode = &#039;&lt;?php echo &quot;Hello, World!&quot;; ?&gt;&#039;;
$highlightedCode = &#039;&lt;pre&gt;&#039; . htmlspecialchars($phpCode) . &#039;&lt;/pre&gt;&#039;;
echo $highlightedCode;
?&gt;