代码之家  ›  专栏  ›  技术社区  ›  Cow

手工博客代码查看/显示?

  •  0
  • Cow  · 技术社区  · 12 年前

    我已经手工编写了我的博客代码,没有wordpress或其他任何东西,我正在尝试制作一个功能,允许我在博客中编写代码并将其显示为: <div>Like This</div> 我可能想得太多了,它可能比我想象的更简单。我想做的只是找到 <code></code> 标记,并替换所有 < 具有 $lt; > 具有 &gt; ,等等 <代码></代码> 标签。但我可以想出一种方法,让它为多个 <代码></代码> 标签。

    然后在代码的最后,我将替换 <代码></代码> 具有 <div class="code"></div>

    有更好的方法吗?谢谢大家!

    2 回复  |  直到 12 年前
        1
  •  2
  •   Tudor    12 年前

    使用 preg_replace 用于更换 <code></code> 具有 <div class="code"></div> htmlentities 对所有html标签进行编码,以便将其视为源代码。

        2
  •  1
  •   Geordee Naliyath    12 年前
    <?php
    $html="<code>this is the first code snippet</code><p>This is a normal paragraph</p><code>this is the second code snippet</code>";
    preg_match_all("'<code>(.*?)</code>'si", $html, $match);
    if ($match) {
        foreach ($match[1] as $snippet) {
                echo htmlspecialchars($snippet, ENT_QUOTES);
                echo "\n";           
        }
     }
    ?>