Last active
February 25, 2016 22:27
-
-
Save dreness/ab0abb07de294d28ca80 to your computer and use it in GitHub Desktop.
exorcise the mind of zalgo from a text file
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
## install composer | |
# mkdir dezalgo ; cd dezalgo | |
## place dezalgo.php (this file) into the new directory | |
# composer require mdwheele/zalgo | |
# php dezalgo.php --infile MyBestIRCLog --outfile MySafeIRCLog | |
<?php | |
require __DIR__ . '/vendor/autoload.php'; | |
use Zalgo\Mood; | |
use Zalgo\Soul; | |
use Zalgo\Zalgo; | |
$shortopts = ""; | |
$shortopts .= "i:"; | |
$shortopts .= "o:"; | |
$shortopts .= "v::"; | |
$longopts = array( | |
"infile:", | |
"outfile:", | |
"verbose::", | |
); | |
$options = getopt($shortopts, $longopts); | |
if (!(isset($options['infile']) && (isset($options['outfile'])))) { | |
print("Error: --infile <file> and --outfile <file> command line options are required.\n"); | |
exit(1); | |
} | |
$infile = $options['infile']; | |
$outfile = $options['outfile']; | |
$v = 0; | |
if (isset($options['verbose'])) { | |
$v = 1; | |
} | |
$v && printf("using infile: %s and outfile: %s\n", $infile, $outfile); | |
$soothed = new Zalgo(new Soul(), Mood::soothed()); | |
$out = fopen($outfile, "w"); | |
$in = fopen($infile, "r"); | |
if ($in) { | |
while (($line = fgets($in)) !== false) { | |
$clean = $soothed->soothe($line); | |
$origlen = mb_strlen($line,'UTF-8'); | |
$cleanlen = mb_strlen($clean,'UTF-8'); | |
$delta = $origlen - $cleanlen; | |
if ($v && ($delta > 0)) { | |
printf("cleaned %d bytes for %s", $delta, $clean); | |
}; | |
fwrite($out, $clean); | |
} | |
fclose($in); | |
fclose($out); | |
} else { | |
printf("couldn't open infile: %s",$infile); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
andre@youbeill[work/dezalgo]echo ".\n.\n.\n.\n.\n.\n.\n" ; head -n 1 ~/work/bad.txt ; echo ".\n.\n.\n.\n.\n.\n"
.
.
.
1 21:32 < RyuKojiro> N̬͙͕̯͇̼̭͇̖͍̘̲̑̅ͣͧ͛̈́̽͌͆ͨ̐̄̀̋ͥ̓̏̚I̠͚̗̟̓ͫ̆̌̍͂͋̎͐̈́C̦͇̯̝̣̬͈͖͊̒̃̏͒ͩ͆ͦ̋ͪ͗̾̑̄͑͌͛ͅO̙͓̙̰̘̮͇͖̺̐̑͛̏̊ͣͧ̾̂̆̅͆ͫ̃̊̆ͧ́ͧO̞̖̲̝̣̯̠̹͎̲͈̐ͬ͛͐̀̌͋̉̄͛ͦ̽̀̓̓Ô͖̪̤̭̟̬̪̥̭̹̏̒͗͋̓͊O͓̤̪̦ͨ̍̃̂̒̑̓͑̈ͬ̒̂́̀͒̓͋̚O̭̬̩̙̗͍̜̹͔̭̱̼̟̟̪̙̿ͣ͑̒̃̆͋ͬ̂̔ͪͅͅO͔̰̦͉̘͙̤̐̆̀̏̒ͪĎ͆̇͒̀͛͑̇ͮ͋́̃͂?
.
.
.
andre@youbeill[work/dezalgo]
andre@youbeill[work/dezalgo]php dezalgo.php --infile ~/work/bad.txt --outfile testout --verbose
using infile: /home/andre/work/bad.txt and outfile: testout
cleaned 218 bytes for 1 21:32 < RyuKojiro> NICOOOOOOD?