Skip to content

Instantly share code, notes, and snippets.

@HoangPV
Created September 18, 2017 03:18
Show Gist options
  • Save HoangPV/f9ce990de627a69a7209c6f19a844667 to your computer and use it in GitHub Desktop.
Save HoangPV/f9ce990de627a69a7209c6f19a844667 to your computer and use it in GitHub Desktop.
Replace With Alphabet Position
<?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