代码之家  ›  专栏  ›  技术社区  ›  Marcy Sutton

如何在<pre><code>

  •  1
  • Marcy Sutton  · 技术社区  · 15 年前

    在我的博客上的代码示例中,我很难保留一个和号,因为所有HTML实体都以&开头。

    有什么小窍门吗?

    例如:

    <pre>
    <code>
    <?php 
    $pageTitle = str_replace('&', ' &amp;', $page->attributes()->title);
    ?>
    </code>
    </pre>
    

    呈现为:

    <?php 
    $pageTitle = str_replace('&', '&', $page->attributes()->title);
    ?>
    
    2 回复  |  直到 9 年前
        1
  •  4
  •   Mark Rushakoff    15 年前

    我不确定是不是 最好的 但一个解决方法是双重逃避:

    str_replace('&', ' &amp;amp;', $page->attributes()->title);
    

    这边,第一个 &amp; 以文字和符号的形式显示,然后 amp; 显示为文本。

        2
  •  2
  •   ZZ Coder    15 年前

    需要使用htmlEntities()对字符串进行编码。例如,

    <pre>
    <code>
    <?php
    echo htmlentities("$pageTitle = str_replace('&', ' &amp;', $page->attributes()->title)");
    ?>
    </code>
    </pre>