How to preserve & in <pre><code>
I am having trouble preserving an ampersand in a code example on my blog, because all HTML entities start with &.
Any tips?
For example:
<pre>
<code>
<?php
$pageTitle = str_replace('&', ' &', $page->attributes()->title);
?>
</code>
</pre>
Renders as:
<?php
$pageTitle = 开发者_StackOverflowstr_replace('&', '&', $page->attributes()->title);
?>
I'm not sure if it's the best option, but one workaround is to double-escape it:
str_replace('&', ' &amp;', $page->attributes()->title);
This way, the first &
shows up as a literal ampersand, then the remaining amp;
shows up as literal text.
You need to encode the string with htmlentities(). For example,
<pre>
<code>
<?php
echo htmlentities("$pageTitle = str_replace('&', ' &', $page->attributes()->title)");
?>
</code>
</pre>
精彩评论