duanlang1196 2018-11-18 19:23
浏览 41

Powershell将语法转换为PHP

I'm trying to organize a string output from "qwinsta" on PHP. On powershell I just have to use "ForEach-Object {$_.Trim() -replace "\s+",","} | ConvertFrom-Csv" and it's done. But how can I do the same "filter" on PhP?

  • 写回答

2条回答 默认 最新

  • dqz84361326 2018-11-18 19:39
    关注

    For the first part, you can use:

    $str = file_get_contents('php://stdin');
    $lines = [];
    foreach (explode(PHP_EOL, $str) as $line) {
        $lines[] = preg_replace('/\s+/', ',', trim($line));
    }
    
    echo implode(PHP_EOL, $lines);
    

    That way you can have CSV data (being able to execute the script something like with qwinsta | php test.php). Now what you want to do further with it depends on your case, I believe you want to use PHP to process it further rather than just print the same way you can do it with Powershell. If you want to access individual variables then you can use an additional explode in foreach loop, exploding using comma as delimeter, and then access individual variables with index (and then also perhaps ship first line which are just names, and optionally use them to describe if you want to convert this stuff, say, to JSON).

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?