是否可以通过php-cli访问剪贴板?对于Windows我特别需要它,但是跨平台的解决方案也将很好。
答案 0 :(得分:1)
可在Windows 7+(PowerShell 2+),基于X.org的linux系统和MacOS上运行的便携式功能:
function getClipboard():string{
if(PHP_OS_FAMILY==="Windows"){
// works on windows 7 + (PowerShell v2 + )
return substr(shell_exec('powershell -sta "add-type -as System.Windows.Forms; [windows.forms.clipboard]::GetText()"'),0,-2);
}elseif(PHP_OS_FAMILY==="Linux"){
// untested! but should work on X.org-based linux GUI's
return substr(shell_exec('xclip -out -selection primary'),0,-1);
}elseif(PHP_OS_FAMILY==="Darwin"){
// untested!
return substr(shell_exec('pbpaste'),0,-1);
}else{
throw new \Exception("running on unsupported OS: ".PHP_OS_FAMILY." - only Windows, Linux, and MacOS supported.");
}
}
关于写入剪贴板:
function setClipboard(string $new):bool{
if(PHP_OS_FAMILY==="Windows"){
// works on windows 7 +
$clip=popen("clip","wb");
}elseif(PHP_OS_FAMILY==="Linux"){
// untested! but should work on X.org-based linux GUI's
$clip=popen('xclip -in -selection primary','wb');
}elseif(PHP_OS_FAMILY==="Darwin"){
// untested!
$clip=popen('pbcopy','wb');
}else{
throw new \Exception("running on unsupported OS: ".PHP_OS_FAMILY." - only Windows, Linux, and MacOS supported.");
}
$written=fwrite($clip,$new);
return (pclose($clip)===0 && strlen($new)===$written);
}
答案 1 :(得分:0)
$ someVar =“ value”;
shell_exec(“ echo $ someVar | clip”);
ref:Copy to clipboard from php command line script in Windows 7