Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to run Ruby/Python scripts from inside PHP passing and receiving parameters?
    primarykey
    data
    text
    <p>I need to turn HTML into equivalent Markdown-structured text. </p> <p>OBS.: <a href="https://stackoverflow.com/questions/4686842/how-to-stdin-and-stdout-with-php-and-python-to-use-html2text-and-get-a-markdown-f">Quick and clear way of doing this with PHP &amp; Python</a>.</p> <p>As I am programming in PHP, some people indicates <strong>Markdownify</strong> to do the job, but unfortunately, the code is not being updated and in fact <strong>it is not working</strong>. At sourceforge.net/projects/markdownify there is a "NOTE: unsupported - do you want to maintain this project? contact me! Markdownify is a HTML to Markdown converter written in PHP. See it as the successor to html2text.php since it has better design, better performance and less corner cases."</p> <p>From what I could discover, I have only two good choices:</p> <ul> <li><p>Python: Aaron Swartz's html2text.py</p></li> <li><p>Ruby: Singpolyma's html2markdown.rb, based on Nokogiri</p></li> </ul> <p>So, from PHP, I need to pass the HTML code, call the Ruby/Python Script and receive the output back.</p> <p>(By the way, a folk made a similar question here ("how to call ruby script from php?") but with no practical information to my case).</p> <p>Following the Tin Man`s tip (bellow), I got to this:</p> <p>PHP code:</p> <pre><code>$t='&lt;p&gt;&lt;b&gt;Hello&lt;/b&gt;&lt;i&gt;world!&lt;/i&gt;&lt;/p&gt;'; $scaped=preg_quote($t,"/"); $program='python html2md.py'; //exec($program.' '.$scaped,$n); print_r($n); exit; //Works!!! $input=$t; $descriptorspec=array( array('pipe','r'),//stdin is a pipe that the child will read from array('pipe','w'),//stdout is a pipe that the child will write to array('file','./error-output.txt','a')//stderr is a file to write to ); $process=proc_open($program,$descriptorspec,$pipes); if(is_resource($process)){ fwrite($pipes[0],$input); fclose($pipes[0]); $r=stream_get_contents($pipes[1]); fclose($pipes[1]); $return_value=proc_close($process); echo "command returned $return_value\n"; print_r($pipes); print_r($r); } </code></pre> <p>Python code:</p> <pre><code>#! /usr/bin/env python import html2text import sys print html2text.html2text(sys.argv[1]) #print "Hi!" #works!!! </code></pre> <p>With the above I am geting this:</p> <p>command returned 1 Array ( [0] => Resource id #17 <a href="https://stackoverflow.com/questions/4686842/how-to-stdin-and-stdout-with-php-and-python-to-use-html2text-and-get-a-markdown-f">1</a> => Resource id #18 )</p> <p>And the "error-output.txt" file says:</p> <p>Traceback (most recent call last): File "html2md.py", line 5, in print html2text.html2text(sys.argv<a href="https://stackoverflow.com/questions/4686842/how-to-stdin-and-stdout-with-php-and-python-to-use-html2text-and-get-a-markdown-f">1</a>) IndexError: list index out of range</p> <p>Any ideas???</p> <hr> <p>Ruby code (<strong>still beeing analysed</strong>)</p> <pre><code>#!/usr/bin/env ruby require_relative 'html2markdown' puts HTML2Markdown.new("&lt;h1&gt;#{ ARGF.read }&lt;/h1&gt;").to_s </code></pre> <hr> <p>Just for the records, I tryed before to use PHP's most simple "exec()" but I got some problemas with some special characters very common to HTML language.</p> <p>PHP code:</p> <pre><code>echo exec('./hi.rb'); echo exec('./hi.py'); </code></pre> <p>Ruby code:</p> <pre><code>#!/usr/bin/ruby puts "Hello World!" </code></pre> <p>Python code:</p> <pre><code>#!usr/bin/python import sys print sys.argv[1] </code></pre> <p>Both working fine. But when the string is a bit more complicated:</p> <pre><code>$h='&lt;p&gt;&lt;b&gt;Hello&lt;/b&gt;&lt;i&gt;world!&lt;/i&gt;&lt;/p&gt;'; echo exec("python hi.py $h"); </code></pre> <p>It did not work at all.</p> <p>That's because the html string needed to have its special characters scaped. I got it using this:</p> <pre><code>$t='&lt;p&gt;&lt;b&gt;Hello&lt;/b&gt;&lt;i&gt;world!&lt;/i&gt;&lt;/p&gt;'; $scaped=preg_quote($t,"/"); </code></pre> <p>Now it works like I said <a href="https://stackoverflow.com/questions/4686842/how-to-stdin-and-stdout-with-php-and-python-to-use-html2text-and-get-a-markdown-f">here</a>.</p> <p>I am runnig: Fedora 14 ruby 1.8.7 Python 2.7 perl 5.12.2 PHP 5.3.4 nginx 0.8.53</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload