$newText = '';
// Detect if color method was already in use with this text
if (strpos($text, "\033[0m"
) !== false
) { $pattern = '/\\033\\[0;.+?\\033\\[0m/u';
preg_match_all($pattern,
$text,
$matches);
$coloredStrings =
$matches[0
];
// No colored string found. Invalid strings with no `\033[0;??`.
if ($coloredStrings ===
[]) { return $newText . self::
getColoredText($text,
$foreground,
$background,
$format);
} $nonColoredText =
preg_replace( $pattern,
'<<__colored_string__>>',
$text );
$nonColoredChunks =
preg_split( '/<<__colored_string__>>/u',
$nonColoredText );