PHP.mk документација

readline

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

function.readline.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека function.readline.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
readline

Референца за `function.readline.php` со подобрена типографија и навигација.

function.readline.php

readline

(PHP 4, PHP 5, PHP 7, PHP 8)

readlineЧита ред

= NULL

readline(?string $prompt = null): string|false

Чита еден ред од корисникот. Мора сами да го додадете овој ред во историјата користејќи readline_add_history().

Параметри

prompt
Може да наведете стринг со кој ќе го прашате корисникот.

Вратени вредности

Враќа еден стринг од корисникот. Вратениот ред го нема крајниот нов ред. Ако нема повеќе податоци за читање, тогаш false се враќа.

Примери

Пример #1 readline() Пример

<?php
//get 3 commands from user
for ($i=0; $i < 3; $i++) {
$line = readline("Command: ");
readline_add_history($line);
}

//dump history
print_r(readline_list_history());

//dump variables
print_r(readline_info());
?>

Белешки од корисници 5 белешки

Antony Penn
пред 6 години
Christian's code works well, but if you want to be able to hide the user input and not echo it to screen, you need to add -s to the read command. The code below is an expanded function that allows an optional prompt and optional hiding of the input:

function read_password($prompt=null, $hide=false)
{
    if($prompt) print $prompt;
    $s = ($hide) ? '-s' : '';
    $f=popen("read $s; echo \$REPLY","r");
    $input=fgets($f,100);
    pclose($f);
    if($hide) print "\n";
    return $input;
}
turdsurfer
пред 9 години
If your CLI script accepts input from STDIN and you also want it to prompt for a password (e.g. as mysql client does), then readline() won't work for you. 
What you need to do is read from the terminal device as shown below.

function readline_terminal($prompt = '') {
    $prompt && print $prompt;
    $terminal_device = '/dev/tty';
    $h = fopen($terminal_device, 'r');
    if ($h === false) {
        #throw new RuntimeException("Failed to open terminal device $terminal_device");
        return false; # probably not running in a terminal.
    }
    $line = rtrim(fgets($h),"\r\n");
    fclose($h);
    return $line;
}
$pass = readline_terminal('Password: ');
cox на idecnet точка com
figroc at gmail dot com
In CGI mode be sure to call:

ob_implicit_flush(true);

at the top of your script if you want to be able to output data before and after the prompt.

-- Tomas V.V.Cox
Анонимен
пред 8 години
a few observations....

I use Cygwin PHP v7 and readline is available. The readline_list_history() function though is not defined.

A prompt with escape sequences are sanitized, so use something like:

<?php
    echo("\e[0m\e[34mPromt>\e[0m");
    $inp = readline(' ');
?>

I have not fully documented it, but I see that sometimes strings beginning with punctuation characters do not make it into the history with readline_add_history(). They also sometimes clear the prompt string.
rojaro на gmail точка com
пред 17 години
Note that readline() will return boolean "false" when the user presses CTRL+D.
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.