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?
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).
$str = file_get_contents('php://stdin');
$lines=explode(" ",$str); foreach ($lines as $line) {
if(strpos($line,"/*expression to filter*/")!=0)
print_r( $line); }