Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7572539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:57:02+00:00 2026-05-30T15:57:02+00:00

What I want to do is to allow users to post code if they

  • 0

What I want to do is to allow users to post code if they need to, so it is viewable and it doesn’t render. For example:

<span>
<div id="hkhsdfhu"></div>
</span>
<h1>Hello</h1>

Should be turned into:

&lt;span&gt;
&lt;div id="hkhsdfhu"&gt;&lt;/div&gt;
&lt;/span&gt;
&lt;h1&gt;Hello&lt;/h1&gt;

Only if it is wrapped in <code></code> tags. Right now I am using the following function to allow only certain HTML tags and escape any other tags:

function allowedHtml($str) {
$allowed_tags = array("b", "strong", "i", "em");
$sans_tags = str_replace(array("<", ">"), array("&lt;","&gt;"), $str);
$regex = sprintf("~&lt;(/)?(%s)&gt;~", implode("|",$allowed_tags));
$with_allowed = preg_replace($regex, "<\\1\\2>", $sans_tags);
return $with_allowed;
}

However, if a user wraps their code in <code></code> tags and it contains any of the allowed tags in my function above, those tags will render instead of being escaped. How can I make it where anything in <code></code> tags gets escaped (or just the < and > turned into &lt; and &gt;)? I know about htmlentities() but I don’t want to do that to the whole post, only stuff inside <code></code> tags.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:57:03+00:00Added an answer on May 30, 2026 at 3:57 pm

    Just use a single preg_replace() function with the e modifier to execute an htmlenteties() function on everything it finds within <code> tags

    EDITED

    function allowedHtml($str) {
      $str = htmlentities($str, ENT_QUOTES, "UTF-8");
      $allowed_tags = array("b", "strong", "i", "em", "code");
      foreach ($allowed_tags as $tag) {
        $str = preg_replace("#&lt;" . $tag . "&gt;(.*?)&lt;/" . $tag . "&gt;#i", "<" . $tag . ">$1</" . $tag . ">", $str);
      }
      return $str;
    }
    
    $reply = allowedHtml($_POST['reply']);
    $reply = preg_replace("#\<code\>(.+?)\</code\>#e", "'<code>'.htmlentities('$1', ENT_QUOTES, 'UTF-8').'</code>'", $reply);
    $reply = str_replace("&amp;", "&", $reply);
    

    Rewrote your allowedHtml() function and added a str_replace() at the end.

    It’s tested and should now work perfectly 🙂

    UPDATED – NEW SOLUTION

    function convertHtml($reply, $revert = false) {
      $specials = array("**", "*", "_", "-");
      $tags = array("b", "i", "u", "s");
    
      foreach ($tags as $key => $tag) {
        $open = "<" . $tag . ">";
        $close = "</" . $tag . ">";
    
        if ($revert == true) {
          $special = $specials[$key];
          $reply = preg_replace("#" . $open . "(.+?)" . $close . "#i", $special . "$1" . $special, $reply);
        }
        else {
          $special = str_replace("*", "\*", $specials[$key]);
          $reply = preg_replace("#" . $special . "(.+?)" . $special . "#i", $open . "$1" . $close, $reply);
        }
      }
    
      return $reply;
    }
    
    $reply = htmlentities($reply, ENT_QUOTES, "UTF-8");
    $reply = convertHtml($reply);
    
    $reply = preg_replace("#[^\S\r\n]{4}(.+?)(?!.+)#i", "<pre><code>$1</code></pre>", $reply);
    $reply = preg_replace("#\</code\>\</pre\>(\s*)\<pre\>\<code\>#i", "$1", $reply);
    
    $reply = nl2br($reply);
    $reply = preg_replace("#\<pre\>\<code\>(.*?)\</code\>\</pre\>#se", "'<pre><code>'.convertHtml(str_replace('<br />', '', '$1'), true).'</code></pre>'", $reply);
    

    Discussed another solution, and the above code will fix that. It works just like the Stack Overflow html conversion, which means that ** becomes bold, * becomes italic, _ becomes underlined and – is “strikethrough”. On top of that, all lines starting with 4 or more spaces will be output as code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to allow users to upload avatar-type images in a variety of formats
I want to allow users to paste <embed> and <object> HTML fragments (video players)
I want to allow users to upload files with transaction information to my application.
I want to allow users of my application to add sub-users and set privileges
I want to allow users to put in am img url instead of uploading
I want to allow users to either have there username field empty at any
I have some labels that I want to allow users to edit. Is it
We have a high security application and we want to allow users to enter
I'm building a CMS in ASP.NET MVC and want to allow users to pick
I'm building a simple web based forum application. I want to allow users to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.