Jump to content

How can I replace my two-spaces indentation to tab indentation?


Vinze

Recommended Posts

Hey, when I was very new to PHP I started this project, and I used two-spaces indentation. However, I have now learnt that it is better to use tab indentation (because I now understand what "tab width" means ;) . Is there are way to replace all my two-spaces indentation to tab indentation?

Thanks in advance.
i would try something like this:
[code]
<?php
$String = preg_replace('|  |', "\t", $String);
?>
[/code]

now, that in and of itself will replace [b]all[/b] two space strings with a tab, so you may want to define a little further in your match to limit it to spaces at the front of a line.
[quote author=obsidian link=topic=108168.msg434829#msg434829 date=1158324078]
i would try something like this:
[code]
<?php
$String = preg_replace('|  |', "\t", $String);
?>
[/code]

now, that in and of itself will replace [b]all[/b] two space strings with a tab, so you may want to define a little further in your match to limit it to spaces at the front of a line.
[/quote]

That's true, I was thinking it should not effect everything else in my files, but how often do I use two spaces? Maybe in my HTML but that's unordered anyway. Now, how do I apply this to [b]all[/b] files in a directory, recursively?
hmm... try something like this (and i'm writing off the cuff here, so it may take some tweaking):
[code]
<?php
// set up your recursive function to start in the declared directory
function recursiveScan($dir) {
  if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
      while ($file = readdir($dh) !== false) {
        // if it's a directory, call this function again!
        if (is_dir($dir . $file)) recursiveScan($dir . $file);
        else {
          $String = file_get_contents($dir . $file);
          $String = preg_replace('|  |', "\t", $String);
          $filename = $dir . $file;

          // attempt to open file. print failure, but continue with loop
          if (!$handle = fopen($filename, 'w')) echo "<span class=\"error\">Error opening $filename</span><br />";
          else {
 
            // attempt to write to file. print failure or success and continue loop
            if (fwrite($handle, $String) === FALSE) echo "<span class=\"error\">Error opening $filename</span><br />";
            else echo "<span class=\"success\">Successfully updated $filename</span><br />";
            fclose($handle);
          }
        }
      }
    }
  }
}
?>
[/code]

then, you should be able to call that function passing it the directory you want to run recursively from. now, you'll want to run a simple style sheet to make your errors and success outputs more readable. something like this should do nicely:
[code]
body {
  font-family: Verdana, Arial, Helvetica, sans-serif;
  font-size: 10px;
  font-weight: bold;
}

.error {
  color: #d80000;
}

.success {
  color: #14d800;
}
[/code]

also, you'll have to make sure that the permissions on all the folders and files you're trying to update are such that your server has write permissions to them.

hope this helps
[quote author=Vinze link=topic=108168.msg434843#msg434843 date=1158325556]
I'm going to try it out, hope it works...
[/quote]
lol... set up a simple test directory with 2-3 control files first... don't do it on your full setup until you've tested it ;)
[quote author=obsidian link=topic=108168.msg434847#msg434847 date=1158325786]
[quote author=Vinze link=topic=108168.msg434843#msg434843 date=1158325556]
I'm going to try it out, hope it works...
[/quote]
lol... set up a simple test directory with 2-3 control files first... don't do it on your full setup until you've tested it ;)
[/quote]

Hehe, I planned to do it on a backup, you're right, 2-3 files would be best ;)
[quote author=Jenk link=topic=108168.msg434856#msg434856 date=1158326737]
use str_replace, not preg_replace. preg_* is resource intensive and should only be used for regular expression matches, not simple string replacements. (As the manual also says..)
[/quote]But it could be used with $pattern = '|^  |' to check for the beginning of the file. And it's not that it needs to go fast anyways...
Here's the output:

[code]Warning: file_get_contents(/opt/lampp/htdocs/recscan1) [function.file-get-contents]: failed to open stream: No such file or directory in /opt/lampp/htdocs/recscan/doit.php on line 10
Successfully updated /opt/lampp/htdocs/recscan1
Successfully updated /opt/lampp/htdocs/recscan1
Successfully updated /opt/lampp/htdocs/recscan1
Successfully updated /opt/lampp/htdocs/recscan1[/code]

recscan is the folder, somehow it assumed the files are called recscan1, I guess the 1 comes from true somewhere, but I can't figure out the problem (partly because I'm also busy on some other code which also needs attention ;)
[quote author=Vinze link=topic=108168.msg434858#msg434858 date=1158326905]
[quote author=Jenk link=topic=108168.msg434856#msg434856 date=1158326737]
use str_replace, not preg_replace. preg_* is resource intensive and should only be used for regular expression matches, not simple string replacements. (As the manual also says..)
[/quote]But it could be used with $pattern = '|^  |' to check for the beginning of the file. And it's not that it needs to go fast anyways...
[/quote]Yes, and that is matching a regular expression.. your point?
[quote author=Jenk link=topic=108168.msg434862#msg434862 date=1158327073]
[quote author=Vinze link=topic=108168.msg434858#msg434858 date=1158326905]
[quote author=Jenk link=topic=108168.msg434856#msg434856 date=1158326737]
use str_replace, not preg_replace. preg_* is resource intensive and should only be used for regular expression matches, not simple string replacements. (As the manual also says..)
[/quote]But it could be used with $pattern = '|^  |' to check for the beginning of the file. And it's not that it needs to go fast anyways...
[/quote]Yes, and that is matching a regular expression.. your point?
[/quote]

the point is that to do what he wants ultimately, he's going to have to use a regular expression of some sort. that's the whole point for recommending preg_replace in the first place. let's try to stay on topic and help him through the issue instead of picking a fight about code preference unless there is an error that needs to be pointed out.

@ Vinze - i just realized that when you call the recursive scan again, you've got to add the slash to the dir name:
[code]
<?php
// change this line:
if (is_dir($dir . $file)) recursiveScan($dir . $file);

// to this:
if (is_dir($dir . $file)) recursiveScan($dir . $file . '/');
?>
[/code]

don't know that it will help your current problem, but it should smooth out the recursive aspect. i'm not sure where the '1' is coming from. i just went through the code and tested it on my machine with outputting the paths of all the directories and sub-directories, and it seems to work fine for me ???
[quote author=AndyB link=topic=108168.msg434870#msg434870 date=1158327544]
It's not the solution to recursive file improvements, but it might be useful to someone reading this thread .... 'beautify' - http://beautifyphp.sourceforge.net/
[/quote]

Yes or [url=http://www.tote-taste.de/X-Project/beautify/index.html]this one[/url].

Isn't there just some software already available that can do what I want?
[quote author=obsidian link=topic=108168.msg434868#msg434868 date=1158327420]
[quote author=Jenk link=topic=108168.msg434862#msg434862 date=1158327073]
[quote author=Vinze link=topic=108168.msg434858#msg434858 date=1158326905]
[quote author=Jenk link=topic=108168.msg434856#msg434856 date=1158326737]
use str_replace, not preg_replace. preg_* is resource intensive and should only be used for regular expression matches, not simple string replacements. (As the manual also says..)
[/quote]But it could be used with $pattern = '|^  |' to check for the beginning of the file. And it's not that it needs to go fast anyways...
[/quote]Yes, and that is matching a regular expression.. your point?
[/quote]

the point is that to do what he wants ultimately, he's going to have to use a regular expression of some sort. that's the whole point for recommending preg_replace in the first place. let's try to stay on topic and help him through the issue instead of picking a fight about code preference unless there is an error that needs to be pointed out.

@ Vinze - i just realized that when you call the recursive scan again, you've got to add the slash to the dir name:
[code]
<?php
// change this line:
if (is_dir($dir . $file)) recursiveScan($dir . $file);

// to this:
if (is_dir($dir . $file)) recursiveScan($dir . $file . '/');
?>
[/code]

don't know that it will help your current problem, but it should smooth out the recursive aspect. i'm not sure where the '1' is coming from. i just went through the code and tested it on my machine with outputting the paths of all the directories and sub-directories, and it seems to work fine for me ???
[/quote]

It doesn't give the error anymore, but [code]Successfully updated /opt/lampp/htdocs/recscan1
Successfully updated /opt/lampp/htdocs/recscan1
Successfully updated /opt/lampp/htdocs/recscan1
Successfully updated /opt/lampp/htdocs/recscan1[/code]

I am on Linux, could that have to do with it?

[code]:/opt/lampp/htdocs/recscan$ ls -R
.:
doit.php  rec1

./rec1:
rec2  two.php

./rec1/rec2:
one.php
[/code]
[quote author=Jenk link=topic=108168.msg434880#msg434880 date=1158328171]
For future reference use the constant DIRECTORY_SEPARATOR for instances such as $dir . DIRECTORY_SEPARATOR . $file
[/quote]

Crap... Another reason to skim through all my code...
[quote author=Jenk link=topic=108168.msg434889#msg434889 date=1158328799]
and another :p

use realpath() to determine if the path exists, and to also return the absolute path. :)
[/quote]

But that's not scattered over my files as "/" is...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.