Skip to content

Instantly share code, notes, and snippets.

@ziafazal
Created October 2, 2019 10:06
Show Gist options
  • Save ziafazal/68914b98ac80b2c57c71ea82d6ec3e22 to your computer and use it in GitHub Desktop.
Save ziafazal/68914b98ac80b2c57c71ea82d6ec3e22 to your computer and use it in GitHub Desktop.
utility method to strip html tags and entities
import re
from django.utils.html import strip_tags
def strip_html_tags(text, strip_entities=True):
"""
Args:
text (str): text having html tags
strip_entities (bool): If set to True html entities are also stripped
Returns (str): Text without any html tags and entities
"""
text = strip_tags(text)
if strip_entities:
text = re.sub(r'&([a-zA-Z]{4,5}|#[0-9]{2,4});', '', text)
return text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment