<?php
function reverse($input) {
$words = preg_split('/(\s+|[.,!?-])/', $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$reverse = array_map(function($word) {
if (preg_match('/[.,!?-]/', $word)) {
return $word;
} else {
return mb_strrev(preg_replace('/[.,!?-]/u', '', $word), 'UTF-8') . preg_replace('/\p{L}+/u', '', $word);
}
}, $words);
return implode('', $reverse);
}
function mb_strrev($input, $encoding = 'UTF-8') {
$length = mb_strlen($input, $encoding);
$reversed = '';
while ($length-- > 0) {
$reversed .= mb_substr($input, $length, 1, $encoding);
}
return $reversed;
}
$input = "Куда идём мы с Пятачком - Большой-большой секрет!";
$out = reverse($input);
echo $out;