php 使用系统剪切板pbcopy and pbpaste

4

PHP doesn't provide system clipboard api, but we can use php's proc_fopen to call shell command pbcopy on MAC OS X to retrieve this function:

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;
    }
}

原文地址:https://stackoverflow.com/questions/29181038/how-to-copy-string-to-system-clipboard-with-php-which-runs-as-client-side

 上面的可以修改为:

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("pipe", "w"), // 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;
    }
}

 

2、

Pasteboard

posted on 2019-05-14 17:16  ZhYQ_note  阅读(568)  评论(0)    收藏  举报

导航