Created
September 18, 2017 03:18
-
-
Save HoangPV/f9ce990de627a69a7209c6f19a844667 to your computer and use it in GitHub Desktop.
Replace With Alphabet Position
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Replace With Alphabet Position | |
* | |
* @author Phan Vu Hoang <vu-hoang.phan@ekino.com> | |
*/ | |
function alphabet_position(string $str) { | |
$ar_pos = []; | |
$str_len = strlen($str); | |
for ($i=0; $i < $str_len; $i++) { | |
$char = substr($str, $i, 1); | |
if (processing(strtolower($char))) { | |
$ar_pos[] = processing(strtolower($char)); | |
} | |
} | |
return implode($ar_pos, ' '); | |
} | |
function processing($char) { | |
$report=[]; // key => val ex. 1 => a. 2 => b | |
$init = 0; //equal a | |
do{ | |
if($init==0){ | |
$letter='a'; | |
}else{ | |
$letter++; | |
} | |
$init++; | |
$report[$letter] = $init; | |
}while($letter!=='z'); | |
if (ctype_lower($char)) { | |
return $report[$char]; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment