mirror of
https://github.com/billz/raspap-webgui.git
synced 2025-03-01 10:31:47 +00:00
Remove ANSI escape sequences w/ stripArtifacts()
This commit is contained in:
parent
12b0d9f6c8
commit
72475b16ef
@ -129,16 +129,33 @@ function saveProviderConfig($status, $binPath, $country, $id = null)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes artifacts from shell_exec string values
|
* Removes artifacts from shell_exec string values and lines with ANSI escape sequences
|
||||||
*
|
*
|
||||||
* @param string $output
|
* @param string|array $output
|
||||||
* @param string $pattern
|
* @param string|null $pattern
|
||||||
* @return string $result
|
* @return string|array $result
|
||||||
*/
|
*/
|
||||||
function stripArtifacts($output, $pattern = null)
|
function stripArtifacts($output, $pattern = null)
|
||||||
{
|
{
|
||||||
$result = preg_replace('/[-\/\n\t\\\\'.$pattern.'|]/', '', $output);
|
if (is_array($output)) {
|
||||||
return $result;
|
return array_map(function ($line) use ($pattern) {
|
||||||
|
return stripArtifacts($line, $pattern);
|
||||||
|
}, $output);
|
||||||
|
}
|
||||||
|
if (!is_string($output)) {
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
$lines = explode("\n", $output);
|
||||||
|
$lines = array_filter($lines, function ($line) use ($pattern) {
|
||||||
|
// remove ANSI escape sequences
|
||||||
|
if (preg_match('/\x1b\[[0-9;]*[a-zA-Z]/', $line)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$line = preg_replace('/[-\/\t\\\\' . preg_quote($pattern, '/') . '|]/', '', $line);
|
||||||
|
return trim($line) !== '';
|
||||||
|
});
|
||||||
|
return implode("\n", $lines);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user