Author Topic: How to Remove HTML Tags From a String  (Read 5407 times)

Offline admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 296
    • View Profile
How to Remove HTML Tags From a String
« on: May 15, 2010, 11:46:11 AM »
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
:

Code: [Select]
<?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:

Code: [Select]
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:


Code: [Select]
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.