You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PHP的htmlentities()编码<code>标签内的所有内容?

How to Implement encode_tags() for Your Blog

Absolutely! You can totally build a custom function like encode_tags($text,"<code>","</code>") to handle this exact need—let me break down how to make it work smoothly for your blog.

The Core Idea

We need to split your content into two distinct parts:

  • Regular HTML content (where tags like <b> should render normally)
  • Content wrapped in <code> tags (which needs full HTML entity encoding so raw code doesn’t get parsed as active HTML)

The Custom Function

Here’s a PHP function that does exactly this. It uses regex to target <code> blocks, encodes their inner content with htmlentities(), and leaves the rest of your HTML untouched:

function encode_tags($text, $openTag, $closeTag) {
    // Escape special regex characters in the tags to avoid parsing errors
    $escapedOpen = preg_quote($openTag, '/');
    $escapedClose = preg_quote($closeTag, '/');
    
    // Regex pattern to match everything between the open and close tags (including newlines)
    $pattern = "/{$escapedOpen}(.*?){$escapedClose}/s";
    
    // Use a callback to process each matched code block
    return preg_replace_callback($pattern, function($matches) use ($openTag, $closeTag) {
        // Encode the inner content of the code block to neutralize HTML
        $encodedContent = htmlentities($matches[1], ENT_QUOTES, 'UTF-8');
        // Reconstruct the tag pair with the encoded content
        return $openTag . $encodedContent . $closeTag;
    }, $text);
}

How to Use It

Let’s test this with a sample blog content snippet to see it in action:

// Your original blog content
$blogContent = 'Check out this <b>bold text</b>! Here\'s a PHP code example:
<code>
echo "<h1>Hello from Code Block!</h1>";
if ($x > 5) {
    echo "X is greater than 5";
}
</code>';

// Process the content with our function
$processedContent = encode_tags($blogContent, '<code>', '</code>');

// Output the result
echo $processedContent;

What You’ll Get

  • The <b>bold text</b> part will render as bold text in the browser (we leave regular HTML tags fully functional).
  • The code inside <code> will have all special characters encoded:
    • < becomes &lt;
    • > becomes &gt;
    • " becomes &quot;
      This means your raw code will display exactly as written, without the browser trying to parse <h1> or other tags inside the code block.

Key Details to Note

  • The s modifier in the regex ensures it matches content across multiple lines (perfect for multi-line code snippets).
  • ENT_QUOTES ensures both single and double quotes are encoded, which is critical for code that uses string literals.
  • Using preg_quote() makes the function safe even if your tags have special characters (though <code> doesn’t, this makes the function reusable for other tag pairs if needed).

内容的提问来源于stack exchange,提问作者Dan B.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:43:11