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

  • SEARCH
  • Home
  • 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 5997949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:20:11+00:00 2026-05-23T00:20:11+00:00

I have a form where an user can post a global notice into the

  • 0

I have a form where an user can post a global notice into the system (for other users to see).
The system outputs HTML directly from the DB (when a user wanto to see a notice).
I’d like to allow some html tags to stay intact and to have the rest of them with htmlspecialchars() applied.
I already tried to apply

 str_replace($search, $replace, htmlspecialchars($str))

strategy but it seems to be really slow. Too slow, actually. And also it’s not safe that will always work, Is there an alternative for this?
I wanted something that did the strip_tags() job except that it, instead of striping tags it would apply htmlspecialchars to the not allowed tags.

ADD(ed) info (by request):

$str can be any size you can think of. I thought of using a big string (1M characters (generated rendomly with some allowed and some unallowed tags inside. All tags had attributes) for the reason of testing one of the worst case scenarios With the logic: If it works like this, it should work for simpler cases.
The server took 5s to process the complete str_replace (with htmlspecialchars). This test was made in my computer that has 2GHz CPU and DDR3 RAM.
both $search and $replace have a total of 7 replacements. Still they do not always work. In some cases $search gives false positives or false negatives.
To clarify, I apply these changes while saving to the DB and not while retrieving from the DB.

  • 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-23T00:20:12+00:00Added an answer on May 23, 2026 at 12:20 am

    You might try this code (should be improved):

    function callback(array $matches) {
        return htmlspecialchars_decode($matches[0]);
    }
    $str = 'some <i>string</i> <b>with</b> tags '
         . '<a href="#">some link</a> '
         . '<img alt="" src="http://sstatic.net/stackoverflow/img/favicon.ico"/><hr/>';
    $str = htmlspecialchars($str);
    $str = preg_replace_callback('#(&lt;(i|a)(?: .+?)?&gt;.*?&lt;/(\1)&gt;|&lt;(?:img)(?: .*?)?/&gt;)#', 'callback', $str);
    echo $str;
    

    Regular expression looks (should look) for 2 types of strings:

    • <tag attributes>content</tag>, with tag part being the same for opening
      an closing tag, and attributes and content being optional
    • <tag attributes/>, with attributes being optional

    Tags are listed in (i|a) part for <tag></tag> types of tags and (?:img) for <tag/> types of tags.

    If it finds matching tags, it passes content to callback() function which converts it back by using htmlspecialchars_decode(). This is necessary for decoding quotes and other encoded characters in the list of attributes.

    I’m not sure if it works in all cases, i.e., if it matches all necessary tags. If this works in general, then pattern and callback() function should be improved so that callback() decodes only <, > characters and list of attributes; content of tags (i.e., some link part in <a href='#'>some link</a>) must not be decoded.

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

Sidebar

Related Questions

I have a user form where users can post comment on articles. i need
I have a view user control that can post form. This control can be
I have an HTML form where the user can type in any number:5, 8,
I have an HTML form that a user can add an arbitrary amount of
If I have form where user can upload files via Ajax (and upload is
I have a form a user can enter their name, then it will add
I have a form where the user can upload a zip file, it works
I have a form where a user can enter the number of students that
On a website if I have a form where the user can input some
I have a form in which the user can choose a component type from

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.