This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
useful_functions.php
94 lines (79 loc) · 1.94 KB
/
useful_functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/*
Author: NewEraCracker
License: Public Domain
*/
/** Array with the paths a dir contains */
function readdir_recursive($dir='.', $show_dirs=false, $ignored=array())
{
// Set types for stack and return value
$stack = $result = array();
// Initialize stack
$stack[] = $dir;
// Pop the first element of stack and evaluate it (do this until stack is fully empty)
while($dir = array_shift($stack))
{
$dh = opendir($dir);
while($dh && (false !== ($path = readdir($dh))))
{
if($path != '.' && $path != '..')
{
// Prepend dir to current path
$path = $dir.'/'.$path;
if(is_dir($path))
{
// Check ignored dirs
if(is_array($ignored) && count($ignored) && in_array($path.'/', $ignored))
continue;
// Add dir to stack for reading
$stack[] = $path;
// If $show_dirs is true, add dir path to result
if($show_dirs)
$result[] = $path;
}
elseif(is_file($path))
{
// Check ignored files
if(is_array($ignored) && count($ignored) && in_array($path, $ignored))
continue;
// Add file path to result
$result[] = $path;
}
}
}
closedir($dh);
}
// Sort the array using simple ordering
sort($result);
// Now we can return it
return $result;
}
/** Normalize a text file by trimming extra whitespace */
function normalize_text_file($f)
{
// Check if we can work with the file
if( is_file($f) && is_readable($f) && is_writable($f) )
{
// Grab content
$content = file($f);
// Check if content was grabbed
if(count($content))
{
// Detect line ending (Windows CRLF or Unix LF)
$eol = ( (strpos($content[0], "\r\n") !== false) ? "\r\n" : "\n" );
$new = '';
// Build new content
foreach($content as $line)
{
$new .= rtrim($line).$eol;
}
$new = trim($new);
// Some files may have intentional whitespace so we'll keep it
if(strlen($new) > 0)
{
// Write the new content
file_put_contents($f,$new);
}
}
}
}