Jquery (Almost) Equivalent of PHP's Strip_Tags()

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

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.

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

Well, it might not be the best way of doing it but after a lot of thinking and researching I managed to get the job with a simple Regex — 'til someone shows up with a complete example of how to do it better:

const text = `<div id="text">
<blockquote>
<p>Lorem ipsum dolor...</p>
<footer>
By <a href="https://www.github.com/someuser"><strong>Some User</strong></a>
in March, 13
</footer>
</blockquote>
</div>`;

const VALID_TAGS = [
'b', 'strong', 'i', 'em', 's', 'a', 'img', 'blockquote', 'ul', 'li'
];

function stripTags( input, allowed ) {
return input.replace( new RegExp( `\\s*<(?!\/?\\b(${allowed.join('|')})\\b)[^>]+>\\s*`, 'gmi' ), '');
}

console.log( stripTags( text, VALID_TAGS ).trim().replace(/(\r\n|\n|\r)/gm, '' ).replace( /\s{2,}/gm, ' ' ) )

jquery on event remove all tags except span

Give this jQuery a try:

$('#str').on('click', function(e) {
$('body').find('*').not('span').each(function() {
$(this).remove();
});
});

You'll have to adjust its target to whatever parent you want to clear the content of, but this should work.

Edit

I've updated it to account for if the element is contained in a span, at some point:

$('#str').on('click', function(e) {
$('body').find('*').not('span').each(function() {
if(!$(this).parents().is('span')) {
$(this).remove();
}
});
});

If you only want to accommodate for a single level up, replace parents() with parent() in the if statement.

Here's a JSFiddle: http://jsfiddle.net/y1r0wg2r/

List of allowable tags on PHP's strip_tags()

If you are worried about XSS attacks than this is the best cheat sheet I ever use.

updated the link

PHP function to strip tags, except a list of whitelisted tags and attributes

As far as I know, the strip_tags solution is about the fastest way to get rid of unwanted tags, and barring 3rd party packages, checking for allowable attributes would be quite easy in DOMDocument,

$string = strip_tags($string,'<b>');
$dom = new DOMDocument();
$dom->loadHTML($string);
$allowed_attributes = array('id');
foreach($dom->getElementsByTagName('*') as $node){
for($i = $node->attributes->length -1; $i >= 0; $i--){
$attribute = $node->attributes->item($i);
if(!in_array($attribute->name,$allowed_attributes)) $node->removeAttributeNode($attribute);
}
}
var_dump($dom->saveHTML());

PHP strip_tags() causing problems with line break-converter?

You’re applying three functions to your string – mysql_real_escape_string, strip_tags and nl2br2. The order should be reversed because mysql_real_escape_string adds a backslash before \n and \r, making the string unable to be processed by nl2br2. If you apply nl2br2 first, strip_tags next and mysql_real_escape_string last, no such problems should arise.

Replace these four lines

$str = "$_POST[indlaeg]";
mysql_real_escape_string($str); // PROTECT FROM SQL INJECTIONS THROUGH SINGLE QUOTES ''
strip_tags($str, '<b><i><a><video><br>'); // REMOVE ALL TAGS EXPECT
$str = nl2br2($str); // CONVERT LINE BREAKS TO <br>

with

$str = $_POST['indlaeg'];
$str = nl2br2($str); // CONVERT LINE BREAKS TO <br>
$str = strip_tags($str, '<b><i><a><video><br>'); // REMOVE ALL TAGS EXCEPT A FEW
$str = mysql_real_escape_string($str); // PROTECT FROM SQL INJECTIONS THROUGH SINGLE QUOTES ''

Removing all non br and span tags?

I used:

$content = $(elem);
$content.find(':not(br, span)').contents().unwrap();
$content.find(':not(br, span)').remove()

The first line unwraps all elements with content (like <span>some text</span>)
the second line will remove all remaining elements without content (like <hr />, <img />, etc.)

Remove all tags in html string except a

Try this:

$(string).find(':not(a)').contents().unwrap()

This will vorky with every piece of html code.

Example: http://jsfiddle.net/E3RWL/1/



Related Topics



Leave a reply



Submit