Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have PHP open the Ruby or Python script via <a href="http://www.php.net/manual/en/function.proc-open.php" rel="nofollow noreferrer"><code>proc_open</code></a>, piping the HTML into STDIN in the script. The Ruby/Python script reads and processes the data and returns it via STDOUT back to the PHP script, then exits. This is a common way of doing things via <code>popen</code>-like functionality in Perl, Ruby or Python and is nice because it gives you access to STDERR in case something blows chunks and doesn't require temp files, but it's a bit more complex.</p> <p>Alternate ways of doing it could be writing the data from PHP to a temporary file, then using <a href="http://www.php.net/manual/en/function.system.php" rel="nofollow noreferrer"><code>system</code></a>, <a href="http://www.php.net/manual/en/function.exec.php" rel="nofollow noreferrer"><code>exec</code></a>, or something similar to call the Ruby/Python script to open and process it, and print the output using their STDOUT. </p> <p>EDIT:</p> <p>See <a href="https://stackoverflow.com/questions/273262/best-practices-with-stdin-in-ruby/273841#273841">@Jonke's answer</a> for "Best practices with STDIN in Ruby?" for examples of how simple it is to read STDIN and write to STDOUT with Ruby. "<a href="https://stackoverflow.com/q/1450393/128421">How do you read from stdin in python</a>" has some good samples for that language.</p> <p>This is a simple example showing how to call a Ruby script, passing a string to it via PHP's STDIN pipe, and reading the Ruby script's STDOUT:</p> <p>Save this as "test.php":</p> <pre><code>&lt;?php $descriptorspec = array( 0 =&gt; array("pipe", "r"), // stdin is a pipe that the child will read from 1 =&gt; array("pipe", "w"), // stdout is a pipe that the child will write to 2 =&gt; array("file", "./error-output.txt", "a") // stderr is a file to write to ); $process = proc_open('ruby ./test.rb', $descriptorspec, $pipes); if (is_resource($process)) { // $pipes now looks like this: // 0 =&gt; writeable handle connected to child stdin // 1 =&gt; readable handle connected to child stdout // Any error output will be appended to /tmp/error-output.txt fwrite($pipes[0], 'hello world'); fclose($pipes[0]); echo stream_get_contents($pipes[1]); fclose($pipes[1]); // It is important that you close any pipes before calling // proc_close in order to avoid a deadlock $return_value = proc_close($process); echo "command returned $return_value\n"; } ?&gt; </code></pre> <p>Save this as "test.rb":</p> <pre><code>#!/usr/bin/env ruby puts "&lt;b&gt;#{ ARGF.read }&lt;/b&gt;" </code></pre> <p>Running the PHP script gives:</p> <pre><code>Greg:Desktop greg$ php test.php &lt;b&gt;hello world&lt;/b&gt; command returned 0 </code></pre> <p>The PHP script is opening the Ruby interpreter which opens the Ruby script. PHP then sends "hello world" to it. Ruby wraps the received text in bold tags, and outputs it, which is captured by PHP, and then output. There are no temp files, nothing passed on the command-line, you could pass a LOT of data if need-be, and it would be pretty fast. Python or Perl could easily be used instead of Ruby.</p> <p>EDIT:</p> <p>If you have:</p> <pre><code>HTML2Markdown.new('&lt;h1&gt;HTMLcode&lt;/h1&gt;').to_s </code></pre> <p>as sample code, then you could begin developing a Ruby solution with:</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> <p>assuming you've already downloaded the HTML2Markdown code and have it in the current directory and are running Ruby 1.9.2.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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