如何使用php将字符串复制到系统剪贴板,php作为客户端运行


How to copy string to system clipboard with php which runs as client side?

我想在MAC OSX上用php(作为客户端脚本运行)将字符串复制到系统剪贴板。

为什么我想要这个功能

我正在写一个php脚本,它在我的MAC OSX上作为客户端脚本运行。它用于将一些文本上传到网站,将一些文本下载到我的本地MAC OSX,我想将这些文本复制到MAC的系统剪贴板。

那么,有没有任何方法可以在MAC OSX上用php将字符串复制到系统剪贴板?

PHP不提供系统剪贴板api,但我们可以使用PHP的proc_fopen在MAC OS X上调用shell命令pbcopy来检索此函数:

echo copy2clipboard('string');
function copy2clipboard($string){
    $descriptorspec = array(
        0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
        1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
        2 => array("file", "a.txt", "a") // stderr is a file to write to
    );
    $process = proc_open('pbcopy', $descriptorspec, $pipes);
    if (is_resource($process)) {
        fwrite($pipes[0], $string);
        fclose($pipes[0]);
        fclose($pipes[1]);
        $return_value = proc_close($process);
        return $return_value;
    }
}

PHP是服务器端的脚本语言,而"剪贴板"是客户端。使用PHP无法实现您的要求。