<?php
function proc_diff($text1, $text2, $options='') {
$descriptors = [
0 => [ 'file', '/dev/null', 'r' ],
1 => [ 'pipe', 'w' ],
2 => [ 'pipe', 'w' ],
3 => [ 'pipe', 'r' ],
4 => [ 'pipe', 'r' ],
];
$command = sprintf('diff %s /proc/self/fd/3 /proc/self/fd/4', $options);
if( ! $p = proc_open( $command, $descriptors, $pipes ) ) {
throw new \Exception('proc_open failed.');
}
fwrite($pipes[3], $text1);
fwrite($pipes[4], $text2);
fclose($pipes[3]);
fclose($pipes[4]);
$ret = [];
$ret['stdout'] = stream_get_contents($pipes[1]);
$ret['stderr'] = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$ret['code'] = proc_close($p);
return $ret;
}
$in1 = "foo\nbar\n";
$in2 = "foo\nbar\nbaz\n";
var_dump(proc_diff($in1, $in2, '-u'));
preferences:
52.27 ms | 402 KiB | 5 Q