Search code examples
shellapache2iso-8859-1php-7.0

How could I prevent escapeshellarg function from escaping accented characters?


PHP native function escapeshellarg is escaping accented character on my machine

$stringWithAccentedChar = 'Hello à è World';
echo escapeshellarg($stringWithAccentedChar);
// Output : Hello World

I tried with locale US or FR, iso-8859-1 or UTF-8. No change.

I think it's a matter of settings because every php tester online I used doesn't escape those characters.

How could I prevent the removal of accented character ?


Solution

  • When escapeshellarg() was stripping my non-ASCII characters from a UTF-8 string, adding the following fixed the problem:

    <?php
    setlocale(LC_CTYPE, "en_US.UTF-8");
    ?>
    

    Source: https://www.php.net/manual/en/function.escapeshellarg.php#99213