How to Remove HTML Tags From a String

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 use.

Instructions

    • 1

      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.

    • 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.

    • 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.

Related Searches:

Resources

Comments

You May Also Like

Related Ads

Featured