jQuery (almost) equivalent of PHP's strip_tags()

戏子无情 提交于 2019-12-17 04:31:49

问题


Is there a jQuery version of this function?

string strip_tags( string $str [, string $allowable_tags ] )

strip all tags and content inside them from a string except the ones defined in the allowable tags string.

like:

var stripped = strip_tags($('#text').html(), '<p><em><i><b><strong><code>');

from:

<div id="text">
  <p> paragraph </p>
  <div> should be stripped </div>
</div>

回答1:


To remove just the tags, and not the content, which is how PHP's strip_tags() behaves, you can do:

var whitelist = "p"; // for more tags use the multiple selector, e.g. "p, img"
$("#text *").not(whitelist).each(function() {
    var content = $(this).contents();
    $(this).replaceWith(content);
});

Try it out here.




回答2:


To remove all tags you can use

$('<div>Content</div>').text()



回答3:


Just use a regular expression:

html.replace( /<.*?>/g, '' );

Done. :)

For the p tag:

html.replace( /<[^p].*?>/g, '' );

For other tags, it gets more complicated.




回答4:


This worked for me:

function strip_tags(str) {
    str = str.toString();
    return str.replace(/<\/?[^>]+>/gi, '');
}



回答5:


Not an actual answer, but a word of caution (depending on what you're trying to do with this):

IMHO, in almost all cases, input sanitization should be done on the server side (in this case, using the native PHP functions). If your intent is to replace PHP functionality with client-side functionality, I would strongly advise against it.

Why?

Just because you're authoring a website, it doesn't mean that:

  1. Your users have JavaScript enabled. If you aren't submitting your form strictly through script (using submit buttons, etc), it still allows users to submit invalid data (such as <script> tags, etc.)
  2. Requests may not actually be initiated by a browser at all, circumventing any JS-based input sanitization.

Again, not really answering your question, but a word of caution based on where you could possibly be headed based on your question :)




回答6:


To remove all tags, could use:

var StrippedString = OriginalString.replace(/(<([^>]+)>)/ig,"");

Code from: Strip HTML Tags in JavaScript




回答7:


Even if this is an old thread i think it could be useful for those who are still looking for an answer.

The Locutus.io function seems to be the best solution:

function strip_tags (input, allowed) {
      allowed = (((allowed || '') + '').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 input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
        return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : ''
      })
}

Example 1:

strip_tags('<p>Kevin</p> <br /><b>van</b> <i>Zonneveld</i>', '<i><b>')

returns 1: 'Kevin <b>van</b> <i>Zonneveld</i>'

Example 2:

strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>')

returns 2: '<p>Kevin van Zonneveld</p>'

Example 3:

strip_tags("<a href='http://kvz.io'>Kevin van Zonneveld</a>", "<a>")

returns 3: "<a href='http://kvz.io'>Kevin van Zonneveld</a>"

Example 4:

strip_tags('1 < 5 5 > 1')

returns 4: '1 < 5 5 > 1'

Example 5:

strip_tags('1 <br/> 1')

returns 5: '1 1'

Example 6:

strip_tags('1 <br/> 1', '<br>')

returns 6: '1 <br/> 1'

Example 7:

strip_tags('1 <br/> 1', '<br><br/>')

returns 7: '1 <br/> 1'




回答8:


UPDATE

Use the following to strip tags while keeping content

$('#text').find('p').contents().unwrap();

This will strip p tag where p is a child element of '#text'.

Check working example at http://jsfiddle.net/YWCsH/




回答9:


You can try this, probably best solution: http://phpjs.org/functions/strip_tags/



来源:https://stackoverflow.com/questions/5601903/jquery-almost-equivalent-of-phps-strip-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!