Skip to content

Instantly share code, notes, and snippets.

@dreness
Last active February 25, 2016 22:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreness/ab0abb07de294d28ca80 to your computer and use it in GitHub Desktop.
Save dreness/ab0abb07de294d28ca80 to your computer and use it in GitHub Desktop.
exorcise the mind of zalgo from a text file
## 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);
}
?>
@dreness
Copy link
Author

dreness commented Jan 24, 2016

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment