Javascript jQuery(几乎)相当于 PHP 的 strip_tags()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5601903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:59:22  来源:igfitidea点击:

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

javascriptjqueryhtmlstrip-tags

提问by Alex

Is there a jQuery version of this function?

是否有此功能的 jQuery 版本?

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

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>

回答by karim79

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

删除标签,而不是内容,这是 PHP 的strip_tags()行为方式,您可以执行以下操作:

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.

在这里尝试一下。

回答by timsly

To remove all tags you can use

要删除您可以使用的所有标签

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

回答by redolent

Just use a regular expression:

只需使用正则表达式:

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

Done. :)

完毕。:)

For the ptag:

对于p标签:

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

For other tags, it gets more complicated.

对于其他标签,它变得更加复杂。

回答by joan16v

This worked for me:

这对我有用:

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

回答by Allan Andrade

To remove all tags, could use:

要删除所有标签,可以使用:

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

Code from: Strip HTML Tags in JavaScript

代码来自:在 JavaScript 中剥离 HTML 标签

回答by Demian Brecht

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

不是一个实际的答案,而是一个警告(取决于你想用这个做什么):

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

恕我直言,在几乎所有情况下,都应该在服务器端进行输入清理(在这种情况下,使用本机 PHP 函数)。如果您打算用客户端功能替换PHP 功能,我强烈建议您不要这样做。

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.
  1. 您的用户启用了 JavaScript。如果您没有严格通过脚本提交表单(使用提交按钮等),它仍然允许用户提交无效数据(例如 <script> 标签等)
  2. 请求实际上可能根本不是由浏览器发起的,从而绕过了任何基于 JS 的输入清理。

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

同样,不是真正回答你的问题,而是基于你的问题可能会去哪里的警告:)

回答by Dario Corbelli

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 functionseems to be the best solution:

Locutus.io功能似乎是最好的解决办法:

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 (
strip_tags('<p>Kevin</p> <br /><b>van</b> <i>Zonneveld</i>', '<i><b>')
, ) { return allowed.indexOf('<' + .toLowerCase() + '>') > -1 ?
strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>')
: '' }) }

Example 1:

示例 1:

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

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

返回 1: 'Kevin <b>van</b> <i>Zonneveld</i>'

Example 2:

示例 2:

strip_tags('1 < 5 5 > 1')

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

返回 2: '<p>Kevin van Zonneveld</p>'

Example 3:

示例 3:

strip_tags('1 <br/> 1')

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

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

Example 4:

示例 4:

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

returns 4: '1 < 5 5 > 1'

返回 4: '1 < 5 5 > 1'

Example 5:

示例 5:

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

returns 5: '1 1'

返回 5: '1 1'

Example 6:

示例 6:

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

returns 6: '1 <br/> 1'

返回 6: '1 <br/> 1'

Example 7:

示例 7:

##代码##

returns 7: '1 <br/> 1'

返回 7: '1 <br/> 1'

回答by Oleg Popov

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

你可以试试这个,可能是最好的解决方案:http: //phpjs.org/functions/strip_tags/

回答by Hussein

UPDATE

更新

Use the following to strip tags while keeping content

使用以下方法在保留内容的同时剥离标签

##代码##

This will strip ptag where pis a child element of '#text'.

这将去除p标签 wherep是 '#text' 的子元素。

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

http://jsfiddle.net/YWCsH/检查工作示例