1

I am sending an HTML text from PHP to Python via STDIN. My objctive is to use Aaron Swartz's script "html2text.py" and to print the result to PHP via STDOUT.

Camarade Jan gave me the word and put me in the right direction. Here's my test:

PHP code:

$t='<p><b>Hello</b><i>world!</i></p>';
$scaped=preg_quote($t,"/")."\n";//\<p\>\<b\>Hello\<\/b\>\<i\>world\!\<\/i\>\<\/p\>
exec('python hi.py '.$scaped,$r);
print_r($r);//result

Python code:

#! /usr/bin/env python
import html2text
import sys
#print html2text.html2text(sys.stdin.read()) #this part of the code didn't work out...
print html2text.html2text(sys.argv[1])

Result:

Array
(
    [0] => **Hello**_world!_
    [1] => 
    [2] => 
)

All the files are in the same directory (under chmod 077). I am using Aaron Swartz's html2text.py version 2.39 and also installed "python-html2text.noarch" on my Fedora 14 (though I couldn't make it work with this last one).

Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65
Roger
  • 8,286
  • 17
  • 59
  • 77

1 Answers1

2

You're just passing the last line to html2text , and you are not using html2text correctly do this instead :

import html2text
import sys

print html2text.html2text(sys.stdin.read())
mouad
  • 67,571
  • 18
  • 114
  • 106