8000 [Console] Special characters on asking question · Issue #36324 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[Console] Special characters on asking question #36324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
YaFou opened this issue Apr 2, 2020 · 4 comments · Fixed by #41113
Closed

[Console] Special characters on asking question #36324

YaFou opened this issue Apr 2, 2020 · 4 comments · Fixed by #41113

Comments

@YaFou
Copy link
Contributor
YaFou commented Apr 2, 2020

Symfony version(s) affected: 5.0.7

Description
In a command, when the user answers a question with special characters with accents (é, è, à, ...); the output doesn't consider these characters and removes them from the string.

How to reproduce

  1. Create a command
  2. In the execute method, write the code below:
$io = new SymfonyStyle($input, $output);
$userInput = $io->ask('Type here');
$io->writeln($userInput);
return 0;
  1. Execute the command and type such as Bonjour à tous !
  2. The output should be Bonjour \ tous !

I've reproduced the project in a repository.

Possible Solution
It's possibly the encoding of my terminal or in the source code.

Additional context
I use the Windows Terminal Preview with Git Bash. And I've tested on the Windows terminal.
image

@lmasforne
Copy link
Contributor

I have tested too, and i think it's a specific windows bash encoding problem because it's work on linux correctly as you can see :
capture-console

@YaFou
Copy link
Contributor Author
YaFou commented Apr 4, 2020

I have tested too, and i think it's a specific windows bash encoding problem because it's work on linux correctly as you can see :
capture-console

I've tested with a simple script like this:

echo 'Type here: ';
$input = readline();
echo $input;

and I have no problem with the encoding as you can see:
image

@YaFou
Copy link
Contributor Author
YaFou commented Apr 7, 2020

After some research, I find where this issue come: QuestionHelper#L125.

$ret = fgets($inputStream, 4096);

Here, it's a simple way to test it:

echo 'Type here: ';
$resource = fopen('php://stdin', 'r');
$input = fgets($resource);
echo $input;

So, finally, the problem comes from PHP and not Symfony. However, there is no solution to avoid to use fgets()?

@benjaminfunk
Copy link
sapi_windows_cp_set(437);

echo 'Type here: ';
$resource = fopen('php://stdin', 'r');
$input = fgets($resource);
echo $input;

Will fix the problem for me. I tried "chcp 437" in my console but it always reset the settings to default 850.

https://bugs.php.net/bug.php?id=76897 Its not a bug from PHP (look comment)

https://stackoverflow.com/questions/8331353/how-to-read-non-ascii-characters-from-cli-standard-input

chalasr added a commit that referenced this issue Jul 6, 2020
This PR was merged into the 3.4 branch.

Discussion
----------

[Console] Fixes question input encoding on Windows

| Q             | A
| ------------- | ---
| Branch?       | 3.4 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #35842, Fix #36324, Fix #37495 and Fix #37278 <!-- prefix each issue number with "Fix #", if any -->
| License       | MIT
| Doc PR        | no <!-- required for new features -->
<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - Never break backward compatibility (see https://symfony.com/bc).
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too.)
 - Features and deprecations must be submitted against branch master.
-->

To ask a question to a user, the [QuestionHelper](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Helper/QuestionHelper.php) use [`fgets`](https://www.php.net/manual/en/function.fgets.php). However, special characters are not supported on Windows with this function (like accents: `é`, `à`, `ö`). The solution is to set a special encoding with [`sapi_windows_cp_get`](https://www.php.net/manual/en/function.sapi-windows-cp-get).

> Before
```php
$stream = fopen('php://stdin', 'r');
$input = fgets($stream);
echo $input;

// input: "Bonjour à tous"
// output: 'Bonjour \ tous" or "Bonjour   tous"
```

> After
```php
// Check if the function exists because it only exists on Windows
if(function_exists('sapi_windows_cp_set')) {
    sapi_windows_cp_get(437);
}

$stream = fopen('php://stdin', 'r');
$input = fgets($stream);
echo $input;

// input: "Bonjour à tous"
// output: 'Bonjour à tous"
```

*Thanks to @bnjmnfnk for the solution 😉*

Commits
-------

4288df4 [Console] Fixes question input encoding on Windows
@chalasr chalasr closed this as completed Jul 6, 2020
nicolas-grekas added a commit that referenced this issue May 7, 2021
This PR was submitted for the 5.x branch but it was squashed and merged into the 5.2 branch instead.

Discussion
----------

[Console] Fix Windows code page support

| Q             | A
| ------------- | ---
| Branch?       | 5.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #37385, Fix #35842, Fix #36324, Fix #37495, Fix #37278
| License       | MIT

Corrects previous fixes that dealt with the mojibake problem on Windows where an OEM code page was applied to an input string and then messed with PHP.internal_encoding setting used by the script. This caused strings with different encodings to be displayed on the console output.

Commits
-------

be68682 [Console] Fix Windows code page support
nicolas-grekas added a commit that referenced this issue May 13, 2021
This PR was merged into the 4.4 branch.

Discussion
----------

[Console] Fix Windows code page support

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #37385, Fix #35842, Fix #36324, Fix #37495, Fix #37278
| License       | MIT

Corrects mojibake problem on Windows where an OEM code page was applied to an input string and then messed with PHP.internal_encoding setting used by the script. This caused strings with different encodings to be displayed on the console output.

Commits
-------

4145278 [Console] Fix Windows code page support
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants
0