Location : magowiz.net >
Cleaner
Cleaner
cleaner.php
<?php
/***********************
[EMAIL PROTECTED]: Ritesh Agrawal
[EMAIL PROTECTED]: Identifies php files that contain leading or trailing
spaces before or after PHP opening or closings tags
***********************/
//Set Source Path
$sourcepath = ".";
//Regex Express to test leading and trailing spaces
define("PRE", "#^[\n\r|\n\r|\n|\r|\s]+<\?php#");
define("POST", "#\?>[\n\r|\n\r|\n|\r|\s]{2,}$#");
//Clear the file Status Cache
clearstatcache();
//============ Code borrowed from php.net ===============
// Replace \ by / and remove the final / if any
$root = ereg_replace( "/$", "", ereg_replace( "[\\]", "/",
$sourcepath ));
// Touch all the files from the $root directory
if( false === m_walk_dir( $root, "check", true )) {
echo "'{$root}' is not a valid directory\n";
}
// Walk a directory recursivelly, and apply a callback on each file
function m_walk_dir( $root, $callback, $recursive = true ) {
$dh = @opendir( $root );
if( false === $dh ) {
return false;
}
while( $file = readdir( $dh )) {
if( "." == $file || ".." == $file ){
continue;
}
call_user_func( $callback, "{$root}/{$file}" );
if( false !== $recursive && is_dir( "{$root}/{$file}" )) {
m_walk_dir( "{$root}/{$file}", $callback, $recursive );
}
}
closedir( $dh );
return true;
}
//============== end ======================
//If file, checks whether there is any leading spaces before opening PHP tag or
// trailing spaces after closing PHP tag
function check( $path ) {
if( !is_dir( $path )) {
$fh = file_get_contents($path);
if(preg_match(PRE, $fh))
echo $path. " -- contains leading spaces \n";
if(preg_match(POST, $fh))
echo $path . " -- contains trailing spaces \n";
}
}
?>Created by magowiz. Last Modification: Friday 04 di Aprile, 2008 22:58:06 CEST by magowiz.





