I am using MVC 3 and Razor View engine.
What I am trying to do
I am making a blog using MVC 3, I want to remove all HTML formatting tags like <p> <b> <i>
etc..
For which I am using the following code. (it does work)
@{
post.PostContent = post.PostContent.Replace("<p>", " ");
post.PostContent = post.PostContent.Replace("</p>", " ");
post.PostContent = post.PostContent.Replace("<b>", " ");
post.PostContent = post.PostContent.Replace("</b>", " ");
post.PostContent = post.PostContent.Replace("<i>", " ");
post.PostContent = post.PostContent.Replace("</i>", " ");
}
I feel that there definitely has to be a better way to do this. Can anyone please guide me on this.
Thanks Alex Yaroshevich,
Here is what I use now..
The regular expression is slow. use this, it's faster:
You can take a look at http://www.dotnetperls.com/remove-html-tags
You can use regular expression.
This article might help you.
Just in case you want to use regex in .NET to strip the HTML tags, the following seems to work pretty well on the source code for this very page. It's better than some of the other answers on this page because it looks for actual HTML tags instead of blindly removing everything between
<
and>
. Back in the BBS days, we typed<grin>
a lot instead of:)
, so removing<grin>
is not an option.:)
This solution only removes the tags. It does not remove the contents of those tags in situations where that might be important -- a script tag, for example. You'd see the script, but the script wouldn't execute because the script tag itself gets removed. Removing the contents of an HTML tag is VERY tricky, and practically requires that the HTML fragment be well formed...
Also note the
RegexOption.Singleline
option. That's very important for any block of HTML. as there's nothing wrong with opening an HTML tag on one line and closing it in another.I'm not saying this is the best answer. It's just an option and it worked great for me.