Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's equivalent to PHP's strip_tags?

Tags:

Python's equivalent to PHP's strip_tags?

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

like image 655
Viet Avatar asked Feb 19 '10 11:02

Viet


2 Answers

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.
like image 126
e-satis Avatar answered Oct 07 '22 08:10

e-satis


Using BeautifulSoup

from BeautifulSoup import BeautifulSoup soup = BeautifulSoup(htmltext) ''.join([e for e in soup.recursiveChildGenerator() if isinstance(e,unicode)]) 
like image 34
John La Rooy Avatar answered Oct 07 '22 07:10

John La Rooy