How do I execute a PHP function for every line in standard input?
<p>With some background in Linux I have come accustomed to these so called <a href="http://www.pement.org/awk/awk1line.txt" rel="nofollow">oneliners</a>. Today I want to convert some HTML-garbage (entities) to plain text with <a href="http://php.net/manual/en/function.html-entity-decode.php" rel="nofollow"><code>html_entity_decode</code></a> from core PHP.</p>

<p>e.g.</p>

<pre><code>&gt;&gt; html_entity_decode( '&amp;gt;' )
'&gt;'
</code></pre>

<p>According to the help of the interpreter, this can be done with the <code>-R</code> option;</p>

<blockquote>
  <p>Usage: php [options] [-f]  [--] [args...] 
  (...)
    -R &lt;code>        Run PHP &lt;code> for every input line
  (...)</p>
</blockquote>

<p>However, often when I think that PHP is great, it gives me reason to think otherwise (pardon the attitude);</p>

<pre><code>seq 10  | php -R 'echo fgets(STDIN);'
2
4
6
8
10
</code></pre>

<p>...as the case often is, stating the problem often leads to a solution. So I figured out the problem while writing this question. The standard in is closed because PHP closes open file handles upon termination (see <a href="http://php.net/manual/en/language.types.resource.php" rel="nofollow">freeing resources</a>) and that throws away every second line due to some <em>!enticing</em> implementation detail.</p>

<p>Thanks for reading, you can always golf my solution if you like...</p>

<hr>

<p>The part about closing file handles automatically is inferred because <em>variables go out of scope</em> as the <em>main is popped from the execution stack</em>. I have yet to find this documented on php.net.</p>