edainworks.com :: VGR :: replacing non-numeric characters with their alphabetic rank.

replacing non-numeric characters in '001215adc054zedrft4578tyhgreds89hjpodiwnvtsrz64457125001215adc054zedrft4578tyhgreds89hjpodiwnvtsrz64457125001215adc054zedrft4578tyhgreds89hjpodiwnvtsrz64457125001215adc054zedrft4578tyhgreds89hjpodiwnvtsrz64457125001215adc054zedrft4578tyhgreds89hjpodiwnvtsrz64457125001215adc054zedrft4578tyhgreds89hjpodiwnvtsrz64457125' with their alphabetic rank...

method 1 : str_replace(arrays)
001215143054265418620457820258718541989810161549231422201918266445712500121514305426541862045782025871854198981016154923142220191826644571250012151430542654186204578202587185419898101615492314222019182664457125001215143054265418620457820258718541989810161549231422201918266445712500121514305426541862045782025871854198981016154923142220191826644571250012151430542654186204578202587185419898101615492314222019182664457125
page generated in 0.025 ms

method 2 : for loop+Ord()
001215143054265418620457820258718541989810161549231422201918266445712500121514305426541862045782025871854198981016154923142220191826644571250012151430542654186204578202587185419898101615492314222019182664457125001215143054265418620457820258718541989810161549231422201918266445712500121514305426541862045782025871854198981016154923142220191826644571250012151430542654186204578202587185419898101615492314222019182664457125
page generated in 0.05 ms

Code :
echo "replacing non-numeric characters in '$input' with their alphabetic rank...<br>";

echo '<hr>method 1 : str_replace(arrays)<br>';
TimerStart();
$from_this = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$to_this = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26');
$new = str_replace($from_this, $to_this, $input);
echo $new;
TimerStop(TRUE); // display elapsed time

echo '<hr>method 2 : for loop+Ord()<br>';
TimerStart();
$input=strtoupper($input);
$ll=strlen($input);
$new='';
for ($i=0;$i<$ll;$i++) if (!is_numeric($c=$input[$i])) $new.=ord($c)-64; else $new.=$c;
echo $new;
TimerStop(TRUE);

Vincent Graux (VGR) for European Experts Exchange and Edaìn Works  back to list of test scripts
Last update 1970-01-01 01:00:00