Created
October 2, 2019 10:06
-
-
Save ziafazal/68914b98ac80b2c57c71ea82d6ec3e22 to your computer and use it in GitHub Desktop.
utility method to strip html tags and entities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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