- array_reverse: documentation ( source)
- preg_match_all: documentation ( source)
- array_merge: documentation ( source)
- implode: documentation ( source)
- array_filter: documentation ( source)
- array_unique: documentation ( source)
<?php
function uniqueNoEmpty($array) {
return array_unique(array_filter($array, 'strlen'));
}
$original_string = 'one two three 喞 喝 four 刷囿 two 跏正 吁';
if (!preg_match_all('~(\p{Han}+)|(\S+)~u', $original_string, $out)) {
echo 'no qualifying strings';
} else {
$singleBytes = uniqueNoEmpty($out[2]) ?? [];
$multiBytes = array_reverse(uniqueNoEmpty($out[1]));
echo implode(' ', array_merge($singleBytes,$multiBytes));
}