News:

To still receiving newsletters from us please subscribe to our Newsletters:
http://tech.groups.yahoo.com/group/developers-Heaven/

Main Menu

How to Remove HTML Tags From a String

Started by admin, May 15, 2010, 05:46:11 AM

Previous topic - Next topic

admin

Removing HTML tags from a string is a useful form of website maintenance and tidying. Let's say a user inputs information into a form that will appear on the user's public profile; if the user were to input the right HTML code, it could break your site's layout. Look no further than MySpace, which created a cottage industry of bad Web design by openly allowing just that. Luckily, removing these tags is relatively straightforward no matter what language you us

Step1
Remove HTML tags from a string using PHP with the following code
:

<?php $htmlstring preg_replace("/<.*?>/", "", $htmlstring); ?>

This method uses regular expressions to identify the start and end of any HTML tag and strip it from the string. Replace $htmlstring with whatever variable you use.

Step 2
Remove HTML tags from a string on ASP.net with the following code:


public htmlstring Strip(htmlstring text)
{ return Regex.Replace(text, @"<(.|\n)*?>", htmlstring.Empty); }

As in the PHP version, replace "htmlstring" with whatever your string's variable is named.

Step 3
Remove HTML tags from a string using JavaScript if you run into problems with either of the above examples:


function removeHTMLTags(htmlstring){
if(htmlstring){
var stringdiv = document.createElement("div");
stringdiv.innerHTML = htmlstring;
if(document.all) { return stringdiv.innerText; }
else { return stringdiv.textContent; }
} }

Again, replace "htmlstring" with your string's variable.