- round: documentation ( source)
- strrev: documentation ( source)
- str_split: documentation ( source)
- ord: documentation ( source)
- join: documentation ( source)
<?php
function makePalindrome($word)
{
$result = 0;
$chars = str_split($word);
$halfWord = round(count($chars)/2, 0, PHP_ROUND_HALF_DOWN);
for($index = 0; $index >= $halfWord; $index++)
{
if($word != strrev($word))
{
$mirrorCharPos = count($chars) - $index - 1;
$mirrorChar = $chars[$mirrorCharPos];
if(isset($mirrorChar))
{
$result += ord($mirrorChar) - ord($char);
$chars[$mirrorCharPos] = $char;
}
$word = join('', $chars);
}else{
break;
}
}
return $result;
}
$wordToTest1 = 'aaa';
echo 'test1: ' . makePalindrome($wordToTest1) . PHP_EOL;
$wordToTest2 = 'abc';
echo 'test2: ' . makePalindrome($wordToTest2) . PHP_EOL;
$wordToTest3 = 'abcde';
echo 'test3: ' . makePalindrome($wordToTest3) . PHP_EOL;