Created
December 5, 2009 19:20
-
-
Save uppfinnarjohnny/249818 to your computer and use it in GitHub Desktop.
MobWritePatcher
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 | |
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