Jump to content


Photo

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


  • Please log in to reply
18 replies to this topic

#1 Vinze

Vinze
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 15 September 2006 - 12:32 PM

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.
WTH? I became a guru by asking questions!

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 15 September 2006 - 12:41 PM

i would try something like this:
<?php
$String = preg_replace('|  |', "\t", $String);
?>

now, that in and of itself will replace all 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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Vinze

Vinze
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 15 September 2006 - 12:43 PM

i would try something like this:

<?php
$String = preg_replace('|  |', "\t", $String);
?>

now, that in and of itself will replace all 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.


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 all files in a directory, recursively?
WTH? I became a guru by asking questions!

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 15 September 2006 - 01:02 PM

hmm... try something like this (and i'm writing off the cuff here, so it may take some tweaking):
<?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);
          }
        }
      }
    }
  }
}
?>

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:
body {
  font-family: Verdana, Arial, Helvetica, sans-serif;
  font-size: 10px;
  font-weight: bold;
} 

.error {
  color: #d80000;
}

.success {
  color: #14d800;
}

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
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 Vinze

Vinze
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 15 September 2006 - 01:05 PM

I'm going to try it out, hope it works...
WTH? I became a guru by asking questions!

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 15 September 2006 - 01:09 PM

I'm going to try it out, hope it works...

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 ;)
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 Vinze

Vinze
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 15 September 2006 - 01:13 PM

I'm going to try it out, hope it works...

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 ;)


Hehe, I planned to do it on a backup, you're right, 2-3 files would be best ;)
WTH? I became a guru by asking questions!

#8 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 15 September 2006 - 01:25 PM

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..)

#9 Vinze

Vinze
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 15 September 2006 - 01:28 PM

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..)

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...
WTH? I became a guru by asking questions!

#10 Vinze

Vinze
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 15 September 2006 - 01:29 PM

Here's the output:

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

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 ;)
WTH? I became a guru by asking questions!

#11 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 15 September 2006 - 01:31 PM

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..)

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...

Yes, and that is matching a regular expression.. your point?

#12 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 15 September 2006 - 01:37 PM

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..)

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...

Yes, and that is matching a regular expression.. your point?


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:
<?php
// change this line:
if (is_dir($dir . $file)) recursiveScan($dir . $file);

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

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 ???
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#13 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 September 2006 - 01:39 PM

It's not the solution to recursive file improvements, but it might be useful to someone reading this thread .... 'beautify' - http://beautifyphp.sourceforge.net/
Legend has it that reading the manual never killed anyone.
My site

#14 Vinze

Vinze
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 15 September 2006 - 01:42 PM

It's not the solution to recursive file improvements, but it might be useful to someone reading this thread .... 'beautify' - http://beautifyphp.sourceforge.net/


Yes or this one.

Isn't there just some software already available that can do what I want?
WTH? I became a guru by asking questions!

#15 Vinze

Vinze
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 15 September 2006 - 01:45 PM

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..)

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...

Yes, and that is matching a regular expression.. your point?


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:
<?php
// change this line:
if (is_dir($dir . $file)) recursiveScan($dir . $file);

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

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 ???


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

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

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

./rec1:
rec2  two.php

./rec1/rec2:
one.php

WTH? I became a guru by asking questions!

#16 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 15 September 2006 - 01:49 PM

For future reference use the constant DIRECTORY_SEPARATOR for instances such as $dir . DIRECTORY_SEPARATOR . $file

#17 Vinze

Vinze
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 15 September 2006 - 01:55 PM

For future reference use the constant DIRECTORY_SEPARATOR for instances such as $dir . DIRECTORY_SEPARATOR . $file


Crap... Another reason to skim through all my code...
WTH? I became a guru by asking questions!

#18 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 15 September 2006 - 01:59 PM

and another :P

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

#19 Vinze

Vinze
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 15 September 2006 - 02:01 PM

and another :P

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


But that's not scattered over my files as "/" is...
WTH? I became a guru by asking questions!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users