- preg_replace_callback: documentation ( source)
- array_reverse: documentation ( source)
- implode: documentation ( source)
- explode: documentation ( source)
<?php
$str = 'This is the string number 123-45-6789-0 which contains 12-34567.';
echo preg_replace_callback('~\d+(?:-\d+)+~', function($m) {
return implode('-', array_reverse(explode('-', $m[0]))); }
,$str);