Reputation: 56
I found this post, the top answer (/\G {2}/
) works perfectly for one line, but I can't get it working multi-line.
It needs to convert 2 spaces to tabs, but only at the beginning of the line. Here's an example: http://pastebin.com/4LQ3FUAs
Any help would be appreciated, got a whole project to convert over...
Upvotes: 1
Views: 724
Reputation: 75222
Your solution is probably better, but here's a pure regex version:
$result = preg_replace('/(?:\G|^) {2}/m', "\t", $subject);
^
in multiline mode allows it to match the first pair of spaces in the line, then \G
chains any subsequent matches in that line to the first one.
Upvotes: 2
Reputation: 56
I couldn't find a regular expression that I could use on a whole file at a time, so I created a little script to recursively iterate through a directory and change each line of each php / js / css file individually, runs very fast though. Just cd to your directory and run the script.
<?php
$dir = getcwd();
$it = new RecursiveDirectoryIterator($dir);
$ii = new RecursiveIteratorIterator($it);
$files = new RegexIterator($ii, '#^(?:[A-Z]:)?(?:/(?!\.Trash)[^/]+)+/[^/]+\.(?:php|css|js)$#Di');
foreach ($files as $file)
{
$name = $file->getPath().'/'.$file->getFileName();
echo $name."\n";
$lines = file($file->getPath().'/'.$file->getFileName());
$length = count($lines);
for ($i = 0; $i < $length; ++$i)
{
$lines[$i] = preg_replace('/\G {2}/', "\t", $lines[$i]);
}
file_put_contents($name, implode('', $lines));
}
?>
Upvotes: 0