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 8512143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:12:05+00:00 2026-06-11T04:12:05+00:00

Based on the strip_tags documentation, the second parameter takes the allowable tags. However in

  • 0

Based on the strip_tags documentation, the second parameter takes the allowable tags. However in my case, I want to do the reverse. Say I’ll accept the tags the script_tags normally (default) accept, but strip only the <script> tag. Any possible way for this?

I don’t mean somebody to code it for me, but rather an input of possible ways on how to achieve this (if possible) is greatly appreciated.

  • 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-06-11T04:12:06+00:00Added an answer on June 11, 2026 at 4:12 am

    EDIT

    To use the HTML Purifier HTML.ForbiddenElements config directive, it seems you would do something like:

    require_once '/path/to/HTMLPurifier.auto.php';
    
    $config = HTMLPurifier_Config::createDefault();
    $config->set('HTML.ForbiddenElements', array('script','style','applet'));
    $purifier = new HTMLPurifier($config);
    $clean_html = $purifier->purify($dirty_html);
    

    http://htmlpurifier.org/docs

    HTML.ForbiddenElements should be set to an array. What I don’t know is what form the array members should take:

    array('script','style','applet')
    

    Or:

    array('<script>','<style>','<applet>')
    

    Or… Something else?

    I think it’s the first form, without delimiters; HTML.AllowedElements uses a form of configuration string somewhat common to TinyMCE’s valid elements syntax:

    tinyMCE.init({
        ...
        valid_elements : "a[href|target=_blank],strong/b,div[align],br",
        ...
    });
    

    So my guess is it’s just the term, and no attributes should be provided (since you’re banning the element… although there is a HTML.ForbiddenAttributes, too). But that’s a guess.

    I’ll add this note from the HTML.ForbiddenAttributes docs, as well:

    Warning: This directive complements %HTML.ForbiddenElements,
    accordingly, check out that directive for a discussion of why you
    should think twice before using this directive.

    Blacklisting is just not as "robust" as whitelisting, but you may have your reasons. Just beware and be careful.

    Without testing, I’m not sure what to tell you. I’ll keep looking for an answer, but I will likely go to bed first. It is very late. :)


    Although I think you really should use HTML Purifier and utilize it’s HTML.ForbiddenElements configuration directive, I think a reasonable alternative if you really, really want to use strip_tags() is to derive a whitelist from the blacklist. In other words, remove what you don’t want and then use what’s left.

    For instance:

    function blacklistElements($blacklisted = '', &$errors = array()) {
        if ((string)$blacklisted == '') {
            $errors[] = 'Empty string.';
            return array();
        }
    
        $html5 = array(
            "<menu>","<command>","<summary>","<details>","<meter>","<progress>",
            "<output>","<keygen>","<textarea>","<option>","<optgroup>","<datalist>",
            "<select>","<button>","<input>","<label>","<legend>","<fieldset>","<form>",
            "<th>","<td>","<tr>","<tfoot>","<thead>","<tbody>","<col>","<colgroup>",
            "<caption>","<table>","<math>","<svg>","<area>","<map>","<canvas>","<track>",
            "<source>","<audio>","<video>","<param>","<object>","<embed>","<iframe>",
            "<img>","<del>","<ins>","<wbr>","<br>","<span>","<bdo>","<bdi>","<rp>","<rt>",
            "<ruby>","<mark>","<u>","<b>","<i>","<sup>","<sub>","<kbd>","<samp>","<var>",
            "<code>","<time>","<data>","<abbr>","<dfn>","<q>","<cite>","<s>","<small>",
            "<strong>","<em>","<a>","<div>","<figcaption>","<figure>","<dd>","<dt>",
            "<dl>","<li>","<ul>","<ol>","<blockquote>","<pre>","<hr>","<p>","<address>",
            "<footer>","<header>","<hgroup>","<aside>","<article>","<nav>","<section>",
            "<body>","<noscript>","<script>","<style>","<meta>","<link>","<base>",
            "<title>","<head>","<html>"
        );
    
        $list = trim(strtolower($blacklisted));
        $list = preg_replace('/[^a-z ]/i', '', $list);
        $list = '<' . str_replace(' ', '> <', $list) . '>';
        $list = array_map('trim', explode(' ', $list));
    
        return array_diff($html5, $list);
    }
    

    Then run it:

    $blacklisted = '<html> <bogus> <EM> em li ol';
    $whitelist = blacklistElements($blacklisted);
    
    if (count($errors)) {
        echo "There were errors.\n";
        print_r($errors);
        echo "\n";
    } else {
        // Do strip_tags() ...
    }
    

    http://codepad.org/LV8ckRjd

    So if you pass in what you don’t want to allow, it will give you back the HTML5 element list in an array form that you can then feed into strip_tags() after joining it into a string:

    $stripped = strip_tags($html, implode('', $whitelist)));
    

    Caveat Emptor

    Now, I’ve kind’ve hacked this together and I know there are some issues I haven’t thought out yet. For instance, from the strip_tags() man page for the $allowable_tags argument:

    Note:

    This parameter should not contain whitespace. strip_tags() sees a tag
    as a case-insensitive string between < and the first whitespace or >.
    It means that strip_tags("<br/>", "<br>") returns an empty string.

    It’s late and for some reason I can’t quite figure out what this means for this approach. So I’ll have to think about that tomorrow. I also compiled the HTML element list in the function’s $html5 element from this MDN documentation page. Sharp-eyed reader’s might notice all of the tags are in this form:

    <tagName>
    

    I’m not sure how this will effect the outcome, whether I need to take into account variations in the use of a shorttag <tagName/> and some of the, ahem, odder variations. And, of course, there are more tags out there.

    So it’s probably not production ready. But you get the idea.

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

Sidebar

Related Questions

Example Case- How add filter to strip all html tags(this code not works, just
I want to update a table based on the values from another table. If
Based on how things are done for my company, we issue updates very very
Based on a grayscale image and an ordered closed polygon (may be concave), I
Based on the recommendation by APIGEE in their RESTful API Design blog post I
Based on an answer from a candidate I have a confusion regarding the functioning
Based on the answer for this question What's the difference between CompositionBatch and catalogs?
Based on a configuration setting I'd like to direct users to a This site
Based on this article I was able to get the FullName to work rather
Based on this question I've created a small application which is catching all debug

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.