Created
June 2, 2020 12:21
-
-
Save peterjaap/81dfa825910effb3301946690b37c1cb to your computer and use it in GitHub Desktop.
Quick & dirty PHP script to find hard-coded Dutch words in a PHP / PHTML codebase (such as Magento)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!file_exists('dutch.txt')) { | |
shell_exec('wget -O dutch.txt https://github.com/OpenTaal/opentaal-wordlist/blob/master/wordlist.txt?raw=true'); | |
} | |
$dict = explode("\n", file_get_contents('dutch.txt')); | |
$blacklisted = ['echo', 'var', 'reviews', 'rating', 'all', 'item', 'int', 'details', 'helper', 'label', 'configurator', 'display', 'var', 'id', 'show', 'a', 'br', 'preview', 'as', 'index', 'content', 'import', 'h', 'am', 'the', 'filter', 'type', 'Google', 'source', 'file', 'x', 'y', 'field', 'html']; | |
$dict = array_diff($dict, $blacklisted); | |
function rsearch($folder, $pattern) { | |
$iti = new RecursiveDirectoryIterator($folder); | |
$results = []; | |
foreach(new RecursiveIteratorIterator($iti) as $file){ | |
if(strpos($file , $pattern) !== false){ | |
$results[] = $file; | |
} | |
} | |
return $results; | |
} | |
$phtmls = rsearch('app', '.phtml'); | |
$phps = rsearch('app', '.php'); | |
$files = array_merge($phtmls, $phps); | |
foreach ($files as $phtml) { | |
$tokens = token_get_all(file_get_contents($phtml)); | |
$tokens = array_filter($tokens); | |
foreach ($tokens as $token) { | |
if (isset($token[1])) { | |
$words = strip_tags($token[1]); | |
$words = explode(" ", $words); | |
$words = array_map(function ($word) { | |
return preg_replace("/[^a-zA-Z]/", "", $word); | |
}, array_filter($words)); | |
$words = array_filter($words); | |
$dutchWords = array_intersect($words, $dict); | |
if (count($dutchWords) > 0) { | |
echo $phtml . PHP_EOL; | |
print_r($dutchWords); | |
echo PHP_EOL; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment