4

I'm trying to write a one-liner to convert html entities present in some files (all html with UTF-8 encoding)

I've tried recode HTML_4.0 file.htm but that also converts non-ASCII characters (it breaks the UTF-8 characters)

In StackOverflow I found something that works for one file:

php -r '$f=@fopen("file.htm", "r");echo html_entity_decode(fread($f, 20000));fclose($f);'

but when I try to make it for multiple files with

for fi in *.htm; do php -r '$f=@fopen("$fi", "r");echo html_entity_decode(fread($f, 20000));fclose($f);';done

I know the problem here is how to "escape" $fi (bash variable) so PHP doesn't read it as a PHP variable. Any advice?

1

2 Answers 2

4

You're very nearly there.

And as it happens, the question isn't about how to do it, it's actually about how to get bash to interpret variables and pass them on.

You've got:

for fi in *.htm; do php -r '$f=@fopen("$fi", "r");echo html_entity_decode(fread($f, 20000));fclose($f);';done

Which has single quotes, with double quotes inside.

Single quotes aren't interpolated inside by bash, but double quotes are.. so if you change it to:

for fi in *.htm; do php -r "\$f=@fopen(\"$fi\",'r');echo html_entity_decode(fread(\$f, 20000));fclose(\$f);"; done

Because we're now using double quotes, we also have to escape the $ signs, otherwise bash thinks they're interpolatable variables, and replaces them out.

2

With bash, (single) quotes ' are used to prevent parameter expansion (variables). So you should inverse single and double quotes usage in your bash call to php, and add escaped doubles quotes for the php command arg:

for fi in *.htm; do fi=\"$fi\"; php -r "\$f=@fopen($fi, 'r');echo html_entity_decode(fread(\$f, 20000));fclose(\$f);"; done

Or, simpler, based on Michael Hampton answer, just do:

for fi in *.htm; do cat $fi | php -R 'echo html_entity_decode($argn);'; done
1
  • A very good answer, It also teachs me a little more of php. Thank you for your time. May 27, 2013 at 17:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .