Skip to content

Instantly share code, notes, and snippets.

@uppfinnarjohnny
Created December 5, 2009 19:20
Show Gist options
  • Save uppfinnarjohnny/249818 to your computer and use it in GitHub Desktop.
Save uppfinnarjohnny/249818 to your computer and use it in GitHub Desktop.
MobWritePatcher
<?php
function MobWritePatcher($text, $patch) {
$pointer = 0;
foreach(explode("\t", $patch) as $step) {
$modifier = substr($step, 0, 1);
$argument = substr($step, 1);
switch($modifier) {
case '=':
$diff_length = $argument;
break;
case '+':
$diff_length = strlen($argument);
$text = substr($text, 0, $pointer).$argument.substr($text, $pointer);
break;
case '-':
$diff_length = 0;
$text = substr_replace($text, '', $pointer, $argument);
break;
}
$pointer += $diff_length;
}
return strlen($text) == $pointer ? $text : FALSE;
}
$text = 'Hej hopp tjo falleralla';
$patch = '=4 +kaka -3 =16';
if( $result = MobWritePatcher($text, $patch))
print $result;
else
print 'Checksum failed';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment