tags:

views:

416

answers:

4

Python's equivalent to PHP's strip_tags?

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

+6  A: 

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.
e-satis
Hey! I'm using Django =)
Viet
+1  A: 

Python doesn't have one built-in, but there are an ungodly number of implementations.

Ignacio Vazquez-Abrams
Actually I Googled with the same query you suggested but those didn't make me happy enough.
Viet
+6  A: 

Using BeautifulSoup

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(htmltext)
''.join([e for e in soup.recursiveChildGenerator() if isinstance(e,unicode)])
gnibbler
You may want to let him know that it's a third party lib.
e-satis
Yup, I know, thanks :)
Viet
+1  A: 

You won't find many builtin Python equivalents for builtin PHP HTML functions since Python is more of a general-purpose scripting language than a web development language. For HTML processing, BeautifulSoup is generally recommended.

Otto Allmendinger