admin管理员组文章数量:1309963
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>
?
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>
?
- FYI, don't use regex because bad things will happen – styfle Commented Sep 13, 2017 at 15:32
3 Answers
Reset to default 5Here's the strip_tags() with allowable tags (from phpjs ).
// 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 mentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
return str.replace(mentsAndPhpTags, '').replace(tags, function ($0, $1) {
return allow.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
}
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 uglyevil
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
- The third parameter is a string like
If you need a proper strip_tags
function, have a look at http://phpjs/functions/strip_tags:535
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.
本文标签: Equivalent striptags of PHP in javascript with allowed tagsStack Overflow
版权声明:本文标题:Equivalent strip_tags of PHP in javascript with allowed tags - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741853517a2401205.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论