用户通过命令行跳过输入与PHP交互

时间:2016-11-23 19:37:52

标签: php command-line-interface stdin fopen

我有3个相同的片段,要求用户通过命令行输入。第一个查询是向用户和第三个询问的,但由于某种原因,第二个代码段虽然代码相同,但输出不正确。以下是输出:

enter image description here

正如您所看到的,它会跳过content 2并直接转到内容3.任何想法出了什么问题?

只要我点击内容1 y,代码就会直接转到Would you like to print content 3

<?php

echo "\n\033[1;35m~~~~~~ CONTENT 1 ~~~~~~\033[0m\n\n";

echo "\033[1;37mWould you like to print content 1? (y/n) - ";
$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
    if ($response == 'y') {
      echo "\033[0m";
      echo "Content 1 print success!";
    }

echo "\n\033[1;35m~~~~~~ CONTENT 2 ~~~~~~\033[0m\n\n"; 

echo "\033[1;37mWould you like to print content 2? (y/n) - ";
$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
    if ($response == 'y') {
      echo "\033[0m";
      echo "Content 2 print success!";

    }

echo "\n\033[1;35m~~~~~~ CONTENT 3 ~~~~~~\033[0m\n\n"; 

echo "\033[1;37mWould you like to print content 3? (y/n) - ";
$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
            if ($response == 'y') {
              echo "\033[0m";
              echo "Content 3 print success!\n";
            }
echo "\033[0m";

?>

使用do / while建议它允许我输入3次但是这次内容2和3输入的请求是重复的!

见这里:

    <?

    echo "\n\033[1;35m~~~~~~ CONTENT 1 ~~~~~~\033[0m\n\n";

    do {
        echo "\033[1;37mWould you like to print content 1? (y/n) - ";
        $stdin = fopen('php://stdin', 'r');

        $response = fgetc($stdin);
    } while (!in_array($response, ['y','n']));
    if ($response == 'y') {
      echo "\033[0m";
      echo "Content 1 print success!";
    }

    echo "\n\033[1;35m~~~~~~ CONTENT 2 ~~~~~~\033[0m\n\n";

    do {
        echo "\033[1;37mWould you like to print content 2? (y/n) - ";
        $stdin = fopen('php://stdin', 'r');

        $response = fgetc($stdin);
    } while (!in_array($response, ['y','n']));
    if ($response == 'y') {
      echo "\033[0m";
      echo "Content 2 print success!";
    }

    echo "\n\033[1;35m~~~~~~ CONTENT 3 ~~~~~~\033[0m\n\n";

    do {
        echo "\033[1;37mWould you like to print content 3? (y/n) - ";
        $stdin = fopen('php://stdin', 'r');

        $response = fgetc($stdin);
    } while (!in_array($response, ['y','n']));
    if ($response == 'y') {
      echo "\033[0m";
      echo "Content 3 print success!\n";
    }


    ?>

enter image description here

可能因为输入有一些空白区域或者它正在捕获换行符吗?对不起,我只是盲目地猜测说实话......

-----更新-----

我可以通过添加一个额外的if语句来强制回显只打印一次,从而使我的代码工作。这是一个黑客,但如果有人能提出更好的解决方案,请告诉我!

    echo "\n\033[1;35m~~~~~~ CONTENT 3 ~~~~~~\n\n";
    $x = "1";
    do {
        if ($x==1){
          echo "\033[1;37mWould you like to print content 2? (y/n) - \033[0m\n";
          $x = $x+1;

        }
        $stdin = fopen('php://stdin', 'r');

        $response = fgetc($stdin);
    } while (!in_array($response, ['y','n']));
    if ($response == 'y') {
      echo "\033[0m";
      echo "Content 3 print success!\n";
    }

1 个答案:

答案 0 :(得分:1)

您的代码在这里工作。无论如何,如果你想获得有效的输入,请使用这个do/while,只接受Y或N作为输入:

echo "\n\033[1;35m~~~~~~ CONTENT 1 ~~~~~~\033[0m\n\n";

do {
    echo "\033[1;37mWould you like to print content 1? (y/n) - ";
    $stdin = fopen('php://stdin', 'r');

    $response = fgetc($stdin);
} while (!in_array($response, ['y','n']));
if ($response == 'y') {
  echo "\033[0m";
  echo "Content 1 print success!";
}