Run-length encoding: Difference between revisions
Content added Content deleted
m (→{{header|C}}: "funciton" -> "function") |
(→{{header|PHP}}: Update since the /e modifier was removed in PHP 7.0, see http://www.php.net/preg_replace Changelog) |
||
Line 3,262: | Line 3,262: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<lang php><?php |
<lang php><?php |
||
function encode($str) |
function encode($str) |
||
{ |
|||
return preg_replace('/(.)\1*/e', 'strlen($0) . $1', $str); |
|||
return preg_replace_callback('/(.)\1*/', function ($match) { |
|||
return strlen($match[0]) . $match[1]; |
|||
}, $str); |
|||
} |
} |
||
function decode($str) |
function decode($str) |
||
{ |
|||
return preg_replace('/(\d+)(\D)/e', 'str_repeat($2, $1)', $str); |
|||
return preg_replace_callback('/(\d+)(\D)/', function($match) { |
|||
return str_repeat($match[2], $match[1]); |
|||
}, $str); |
|||
} |
} |
||