Is there a jQuery version of this function?
string strip_tags( string $str [, string $allowable_tags ] )
stri
To remove all tags you can use
$('<div>Content</div>').text()
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.
Just use a regular expression:
html.replace( /<.*?>/g, '' );
Done. :)
For the p
tag:
html.replace( /<[^p].*?>/g, '' );
For other tags, it gets more complicated.
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'.
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:
Again, not really answering your question, but a word of caution based on where you could possibly be headed based on your question :)
To remove all tags, could use:
var StrippedString = OriginalString.replace(/(<([^>]+)>)/ig,"");
Code from: Strip HTML Tags in JavaScript