Skip to content

Instantly share code, notes, and snippets.

@peterjaap
Created June 2, 2020 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterjaap/81dfa825910effb3301946690b37c1cb to your computer and use it in GitHub Desktop.
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)
<?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