Answer the question
In order to leave comments, you need to log in
Can you help with one regular expression?
You need to find 2 spaces in a row and replace them with \t
But, you need to replace only those spaces that go before the beginning of any other characters.
For example, the string:
(4-spaces here)Text(4-spaces here too)
Should be replaced with this:
\t\tText(4-spaces here)
If you use $str = preg_replace('/^ {2}/m', "\t", $str);
That, you get "\t(4-spaces here)Text(4-spaces here) -space)"
Any ideas?
-----------
UPD:
I don't think it's really possible to implement this just using RT.
So the question is removed. Thanks to all!
Answer the question
In order to leave comments, you need to log in
You can add your own logic to the regular expression in PHP.
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
);
}
was too lazy, left it like that
$str = preg_replace('/\n {4}/',"\n\t", $str);
$str = preg_replace('/\n {4}/',"\n\t", $str);
$str = preg_replace('/\n {4}/',"\n\t", $str);
$str = preg_replace('/\n {4}/',"\n\t", $str);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question