-
Notifications
You must be signed in to change notification settings - Fork 1
/
RemovePhpEndTags.php
50 lines (40 loc) · 1.28 KB
/
RemovePhpEndTags.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
<?php
if ($argc == 2) {
$path = $argv[1];
} else {
$path = '.';
}
// find all .php files recursively
$files = array();
$Directory = new RecursiveDirectoryIterator($path);
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
foreach ($Regex as $r) {
$files[] = $r[0];
}
echo "Starting to process ".count($files)." files.\n";
foreach ($files as $file) {
$content = file_get_contents($file);
// if there are no end tags, don't do anything
if (strpos($content, '?>') === false) {
echo "skipping because there are no end tag: $file\n";
continue;
}
preg_match_all('/\?\>/', $content, $matches);
if (count($matches[0]) > 1) {
echo "skipping because of >1 end tags found, maybe not a php-only file: $file\n";
continue;
}
$newContent = rtrim($content);
$newContent = preg_replace('/\?\>$/', '', $newContent, -1, $count);
if ($count == 1) {
$newContent = rtrim($newContent);
$newContent .= "\n";
file_put_contents($file, $newContent);
echo "fixed file: $file\n";
continue;
} else {
echo "skipping because the end tag is not at the end of the file: $file\n";
continue;
}
}