$str = preg_replace('/^ {2}/m', "\t", $str);
function replaceSpacesWithTabs($line, $perTab = 2)
{
return preg_replace_callback('/^[ ]+/',
function ($matches) use ($perTab) {
$len = strlen($matches[0]);
$out = str_repeat("\t", floor($len / $perTab));
if (($rem = $len % $perTab) > 0) {
$out .= str_repeat(' ', $rem);
}
return $out;
},
$line
);
}