2

With some background in Linux I have come accustomed to these so called oneliners. Today I want to convert some HTML-garbage (entities) to plain text with html_entity_decode from core PHP.

e.g.

>> html_entity_decode( '>' )
'>'

According to the help of the interpreter, this can be done with the -R option;

Usage: php [options] [-f] [--] [args...] (...) -R <code> Run PHP <code> for every input line (...)

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

seq 10  | php -R 'echo fgets(STDIN);'
2
4
6
8
10

...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 freeing resources) and that throws away every second line due to some !enticing implementation detail.

Thanks for reading, you can always golf my solution if you like...


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

1 Answers1

2

Keep the standard in open, read all lines with one script (iirc as you would in a similar Perl oneliner);

wget https://www.mountaingoatsoftware.com/agile/user-stories -O- |
    html2text -nobs -style pretty -width 120  |
    php -r 'while ($ii = fgets(STDIN))
                echo html_entity_decode($ii);'

Hooray!