6

I'm trying to pass UTF-8 text as an argument to a command line program using php's exec function and have been having problems. I suspect it's due to character encoding issues.

When I call locale charmap from the command line, I get: UTF-8.

But when I do:

<?php echo exec('locale charmap'); ?>

I get

ANSI_X3.4-1968

How can I change this? It's not something I want to set once -- I'll be working with different languages / encodings potentially so each call to exec() could require a different one. But even knowing how to set it to UTF-8 once would be helpful.

1 Answer 1

14

Prefix the command with LANG = <language>.UTF8.

Example:

$ php -r "echo exec('LANG=\"en_US.UTF8\" locale charmap');"
UTF-8
$ php -r "echo exec('LANG=\"en_US.iso88591\" locale charmap');
ISO-8859-1

You should have the locale installed.

Sign up to request clarification or add additional context in comments.

1 Comment

Life saver. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.