Usi Usi
Usi Usi

Reputation: 2997

Equivalent strip_tags of PHP in javascript with allowed tags

I used this code to strip all tags but I wan't to save some tags like <img> and so on... How can I do? I can't understand how can I filter tags

/***************************************************
    STRIP HTML TAGS
    ****************************************************/
    function strip_tags(html){
 
        //PROCESS STRING
        if(arguments.length < 3) {
            html=html.replace(/<\/?(?!\!)[^>]*>/gi, '');
        } else {
            var allowed = arguments[1];
            var specified = eval("["+arguments[2]+"]");
            if(allowed){
                var regex='</?(?!(' + specified.join('|') + '))\b[^>]*>';
                html=html.replace(new RegExp(regex, 'gi'), '');
            } else{
                var regex='</?(' + specified.join('|') + ')\b[^>]*>';
                html=html.replace(new RegExp(regex, 'gi'), '');
            }
        }
 
        //CHANGE NAME TO CLEAN JUST BECAUSE 
        var clean_string = html;
 
        //RETURN THE CLEAN STRING
        return clean_string;

EDIT** This is my HTML code

<body class="portrait" onLoad="prepareImages()">
    <div id="title_wrapper"><h2 id="title"><a href="[[[LINK]]]">[[[TITLE]]]</a></h2></div>
    <h2 id="subtitle">[[[DATE]]]</h2>
     <div id="content">
        [[[FULL CONTENT]]] etc....
    </div>

I used your function in this way (what I must replace is the: [[[FULL CONTENT]]] etc....)

(strip_tags(contentElem,"<img>");

without results. How can I rewrite the [[[FULL CONTENT]]] etc.... with the [[[FULL CONTENT]]] etc.... without html tags except <img> ?

Upvotes: 2

Views: 7351

Answers (3)

Thomas Allen
Thomas Allen

Reputation: 393

This operation is far simpler if you use the DOM. I don't know why people are trying to use regular expressions for this.

/**
 * Removes all tags with the provided tagName.
 * @param {Element} el The root element.
 * @param {string} tagName The tagName to match.
 * @example
 *   >> document.body.innerHTML;
 *   "<p><img src="foo.jpg">Some <strong>text</strong></p>"
 *   >> stripTags(document.body, 'img');
 *   undefined
 *   >> document.body.innerHTML;
 *   "<p>Some <strong>text</strong></p>"
 *   >> stripTags(document.body, 'strong');
 *   undefined
 *   >> document.body.innerHTML;
 *   "<p>Some text</p>"
 */
function stripTags(el, tagName) {
  var els = el.getElementsByTagName(tagName.toUpperCase());
  for (var i = 0; i < els.length; i++) {
    while (els[i].firstChild)
      els[i].parentNode.insertBefore(els[i].removeChild(els[i].firstChild), els[i]);
    els[i].parentNode.removeChild(els[i--]);
  }
}

That will remove all tags (not their contents), which I think is how strip_tags behaves.

Upvotes: -1

CoursesWeb
CoursesWeb

Reputation: 4237

Here's the strip_tags() with allowable tags (from phpjs.org ).

// allow can be a string like '<b><i>'
function strip_tags(str, allow) {
  // making sure the allow arg is a string containing only tags in lowercase (<a><b><c>)
  allow = (((allow || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');

  var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi;
  var commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
  return str.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
    return allow.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
  });
}

Upvotes: 5

Lekensteyn
Lekensteyn

Reputation: 66425

Eval? Ugh, that is really ugly code. It matches all tags by using a regular expression pattern.

  • If the function call has less than 3 parameters, it just strips all tags.
  • If the function call has at least 3 parameters:
    • The third parameter is a string like "a", "b", "strong". The quotes are required, thanks to the ugly evil eval construct.
    • if the second parameter is a truth-value (true for example), the third parameter is the list of tags that are allowed
    • if the second parameter is a false-value (false for example), the third parameter is the list of tags that are denied

If you need a proper strip_tags function, have a look at http://phpjs.org/functions/strip_tags:535

Upvotes: 3

Related Questions