Version: 1.0
Type: Sample Code (HOWTO)
Category: Other
License: GNU General Public License
Description: This code fragment takes a linux path and searches for \r\n into the specified files. It would then replace \r\n with \n. The same logic could be extended to become a global search and replace as the code supports recusring into directories.
<?php
/*
////////////////////////////////////////
File : rnton.php
Description: Traverses through a specified
directory and converts all
occurances of \r\n to \n,
in the specified type of
files.
Why? : We developed this since after
changing to the new version of
PhpEditor, most of our files
on the Linux machine were
showing double line spacing :)
Version: 1.0
Date: April 26, 2001
By: Nirav Mehta
nirav@magnet-i.com
http://www.magnet-i.com
////////////////////////////////////////
*/
if ($startingPath == "")
{
echo "<h1>Please enter the path to start work from</h1>";
echo "<form><pre>
Starting Linux Path: <input type='text' name='startingPath' size='20'>
<input type='checkbox' name='recurse' value='1'>Recurse into directories?
<input type='submit'>
Note: The files in the directories would have to be chmod 777 to be written back.
The directories would have to be chmod 755 to be read.
</pre>
</form>
";
}
else
{
$ListFormats = array ("php","php3","htm","html");
function GetFiles($dir)
{
GLOBAL $FileList,$ListFormats,$recurse;
if (!is_dir($dir))
{
echo "$dir is not a directory.<br>";
return $ListFormats;
}
echo "-> trying $dir<br>";
$hDirectory = opendir($dir);
if ($hDirectory)
{
/* Read this directory.*/
while ( $strFileName = @readdir($hDirectory) )
{
/* Check for . or .. */
if ( $strFileName != '.' && $strFileName != '..' )
{
$fullName = $dir . '/' . $strFileName;
if (is_dir($fullName) && $recurse == 1)
{
$FileListSubDir[] = GetFiles($fullName); // This is recursion... be carefull
foreach($FileListSubDir as $value)
$FileList[] = $value;
unset($FileListSubDir);
}
/* Walk list of file format extensions.*/
while ( list($strIndex, $strExtension) = each($ListFormats) )
{
/* Prepend period.*/
$strExtension = '.' . $strExtension;
$nExtensionLength = strlen($strExtension);
/* Is this a valid file? If yes, add to list.*/
if (strlen($strFileName) > $nExtensionLength
&& strtolower(substr($strFileName, -$nExtensionLength)) == $strExtension
&& is_file($fullName))
{
$FileList[] = $fullName;
}
}
reset($ListFormats);
}
}
/* Close the directory now.*/
closedir($hDirectory);
}
return $FileList;
}
function convertRNtoN($fileName)
{
// get contents of the file into a string
$fd = fopen ($fileName, "r");
$contents = fread ($fd, filesize ($fileName));
fclose ($fd);
// Find & Replace the \r\n with \n
if (ereg("\r\n",$contents))
{
echo ".........Found Carriage Return in $fileName<br>";
$contents = ereg_replace("\r\n","\n",$contents);
$save = 1;
}
else
$save = 0;
if ($save)
{
$fd = fopen ($fileName, "w");
@flock($fd,"LOCK_EX"); // lock file
@fwrite ($fd,$contents); // write to file
@flock($fd,"LOCK_UN"); // release lock
fclose ($fd);
echo ".........Replaced with New Line in $fileName<br>";
}
return true;
}
// Let's call the function and get the list of the files
// First let's remove the trailing slash
if (substr($startingPath,strlen($startingPath)-1,1) == "/")
$startingPath = substr($startingPath,0,strlen($startingPath)-1);
$AllFile = GetFiles($startingPath);
if (is_array($AllFile))
{
foreach ($AllFile as $fileName)
{
if (!is_array($fileName) && $fileName != "")
{
convertRNtoN($fileName);
}
}
}
else
echo "No files found. $AllFile";
}
?>