Python's equivalent to PHP's strip_tags?

Viet picture Viet · Feb 19, 2010 · Viewed 12.3k times · Source

Python's equivalent to PHP's strip_tags?

http://php.net/manual/en/function.strip-tags.php

Answer

e-satis picture e-satis · Feb 19, 2010

There is no such thing in the Python standard library. It's because Python is a general purpose language while PHP started as a Web oriented language.

Nevertheless, you have 3 solutions:

  • You are in a hurry: just make your own. re.sub(r'<[^>]*?>', '', value) can be a quick and dirty solution.
  • Use a third party library (recommended because more bullet proof) : beautiful soup is a really good one and there is nothing to install, just copy the lib dir and import. Full tuto with beautiful soup.
  • Use a framework. Most Web Python devs never code from scratch, they use a framework such as django that does automatically this stuff for you. Full tuto with django.