略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: readline_list_history

2024-11-15

readline_list_history

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

readline_list_history获取命令历史列表

说明

readline_list_history(): array

获取整个命令行历史.

返回值

返回整个命令行历史的数组.元素的整数索引从零开始

add a noteadd a note

User Contributed Notes 2 notes

up
6
info () gaj ! design
5 years ago
I just noticed that all readline functions are available with my php.exe (PHP 7, Cygwin) except for this one. It would be nice to have it so duplicate lines can be screened.

So to emulate it, I keep a working copy of the history in an array (yeah, extra code/data, but there are ways to keep the history from getting too large).

Loading is like:

<?php
    readline_read_history
(HISTFILE);
   
$hist = file(HISTFILE,FILE_IGNORE_NEW_LINES);
   
array_shift($hist);
?>

Adding is like:

<?php
   
if (!in_array($line,$hist)) {
       
$hist[] = $line;
       
readline_add_history($line);
    }
?>

(One may want to just check the last entry being the same.)
up
2
Anonymous
11 years ago
Note this function is only available when PHP is compiled with libreadline, not if it is compiled with libedit.

<?php
if (function_exists('readline_list_history')) {
 
$history = readline_list_history();
 
// ...
} else {
  echo
'Not supported by the compiled library.'.PHP_EOL;
}
?>

官方地址:https://www.php.net/manual/en/function.readline-list-history.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3