- mb_detect_encoding: documentation ( source)
- mb_substr: documentation ( source)
- array_reverse: documentation ( source)
- mb_strlen: documentation ( source)
- explode: documentation ( source)
- ord: documentation ( source)
- join: documentation ( source)
<?php
function reverseHebrew($text)
{
$words = array_reverse(explode(' ', $text));
foreach ($words as $index => $word) {
if (isHebrew($word)) {
$words[$index] = mbStrRev($word);
}
}
return join(' ', $words);
}
function isHebrew($text)
{
for ($i = 0, $cnt = strlen($text); $i < $cnt; ++$i) {
if (ord($text[$i]) > 127) {
return true;
}
}
return false;
}
function mbStrRev($string, $encoding = null)
{
if ($encoding === null) {
$encoding = mb_detect_encoding($string);
}
$length = mb_strlen($string, $encoding);
$reversed = '';
while ($length-- > 0) {
$reversed .= mb_substr($string, $length, 1, $encoding);
}
return $reversed;
}
$name = "אַבְרָהָם";
$newName = "Alexander Manukian";
echo reverseHebrew($newName);