O'Reilly logo

PHP 5 Kochbuch, Third Edition by Stephan Schmidt, Ulrich Speidel, Matthias Brusdeylins, Carsten Lucke, Adam Trachtenberg, David Sklar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

23.3 Von der Tastatur lesen

Problem

Sie müssen eine getippte Benutzereingabe einlesen.

Lösung

Verwenden Sie fopen( ) mit dem Spezialdateinamen php://stdin:

print "Tippen Sie Ihre Nachricht. Schreiben Sie '.' in einer neuen Zeile,
                                            wenn Sie fertig sind.\n";

$fh = fopen('php://stdin','r') or die($php_errormsg);
$last_line = false;  $message = '';
while (! $last_line) {
    $next_line = fgets($fh,1024);
    if (".\n" == $next_line) {
      $last_line = true;
    } else {
      $message .= $next_line;
    }
}

print "\nIhre Nachricht ist:\n$message\n";

Wenn die Readline-Erweiterung installiert ist, verwenden Sie readline( ):

$last_line = false; $message = ''; while (! $last_line) { $next_line = readline(); if ('.' == $next_line) { $last_line = true; } else { $message .= $next_line."\n"; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required