How to STDIN and STDOUT with PHP and Python to use html2text and get a markdown formated text?
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.开发者_C百科..
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).
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())
精彩评论