Strip tags in Rails, Javascript and PHP

By , August 28, 2009 10:30 pm

Strip tag function is used to remove html tags from the string.

Here I will show you that how to use strip tag in ruby on rails, javascript and php.

#PHP:

Example 1: Remove all html tags.

<?php
$str '<h1>Test1.</h1><p> text2</p>';
echo strip_tags($str);
?>

Example 2: Allow p and h1 html tag.

<?php
$str '<h1>Test1.</h1><p> text2</p>';
echo strip_tags($str,"<h1><p>");
?>

#Ruby On Rails

Example 1: Remove all html tags

#model

class User < ActiveRecord::Base
include ActionView::Helpers
before_save :strip_comment
def strip_comment
return true if self.comment.nil?
self.comment=sanitize(self.comment,:tags =>%w())
save
return true
end

Example 2: Allow h1 and p html tags

#model

class User < ActiveRecord::Base
include ActionView::Helpers
before_save :strip_comment

def strip_comment
return true if self.comment.nil?
self.comment=sanitize(self.comment,:tags =>%w(p h1))
save
return true
end

#Javascript
#Add strip_tags.js

function strip_tags (str, allowed_tags) {

var key = '', allowed = false;
var matches = [];
var allowed_array = [];
var allowed_tag = '';
var i = 0;
var k = '';
var html = '';
var replacer = function (search, replace, str) {
return str.split(search).join(replace);
};

// Build allowes tags associative array
if (allowed_tags) {
allowed_array = allowed_tags.match(/([a-zA-Z0-9]+)/gi);
}

str += '';

// Match tags
matches = str.match(/(<\/?[\S][^>]*>)/gi);

// Go through all HTML tags
for (key in matches) {
if (isNaN(key)) {
// IE7 Hack
continue;
}

// Save HTML tag
html = matches[key].toString();

// Is tag not in allowed list? Remove from str!
allowed = false;

// Go through all allowed tags
for (k in allowed_array) {
// Init
allowed_tag = allowed_array[k];
i = -1;

if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}

// Determine
if (i == 0) {
allowed = true;
break;
}
}

if (!allowed) {
str = replacer(html, "", str); // Custom replace. No regexing
}
}

return str;

}

#Used strip_tags function to remove html tags.
Example 1: remove all html tags.

var html_string= '<h1>Test1.</h1><p> text2</p>';
var strip_string= strip_tags(html_string);

Example 2: allow only p and h1 html tags

var html_string= '<h1>Test1.</h1><p> text2</p>';
var strip_string= strip_tags(html_string,"<p><h1>");
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Webnews
  • Digg
  • del.icio.us
  • Reddit
  • Bloglines
  • LinkedIn
  • YahooMyWeb
  • Facebook
  • Google Bookmarks
  • Mixx
  • MySpace
  • Technorati
  • TwitThis

5 Responses to “Strip tags in Rails, Javascript and PHP”

  1. very cool & good codes, thank you very much for sharing.

    Can I share these codes on my JavaScript library?

    Awaiting your response. Thank

  2. Brijesh Shah says:

    Yes you can share this code. There is no problem for me.

  3. filad says:

    Sorry, i wanted to say : pre (with tags) :D

    (So , in strip_tags.js line 9 there is a pre tag )

Leave a Reply

*

Panorama Theme by Themocracy