...

View Full Version : help me with that function please



crazy.works
03-10-2009, 05:43 PM
hello ,
i was trying to make function to insert into number small md5 hash to make it hard to read for special usage , so lets say i have number like "1234" , with my function it gonna be (1xx2xx3xx4xx) ==> x is md5 num

so i coded the function like that



function code($num) {
$count = strlen($num);
$hash = substr(md5(rand(0,999)), 15, 2);
$start = "0";
while($count > 0) {
$cut = substr($num, $start, 1);
$all = $cut.$hash;
++$start;
--$count;
}
return $all ;
}



but it doesnt return with the full number, it returns with the last x number cazue of while , and if i added echo $all; between while tags it gonna print the all number like that



$all = $cut.$hash;
echo $all;
++$start;
--$count;



so please how can i make the function returns with the full number $cut.$hash (1xx2xx3xx4xx) ??

thanks

oesxyl
03-10-2009, 06:41 PM
try to append not to replace:


$all .= $cut . $hash;


best regards

crazy.works
03-11-2009, 04:01 AM
it doesnt work , any other suggestion please !

oesxyl
03-11-2009, 04:12 AM
it doesnt work , any other suggestion please !
you didn't test it, isn't it?

regards

_Aerospace_Eng_
03-11-2009, 04:13 AM
$start should be an int not a string though php should convert it on the fly. This seems to work

<?php
function code($num)
{
$all = '';
$count = strlen($num);
$hash = substr(md5(rand(0,999)), 15, 2);
while($count > 0)
{
$cut = substr($num, $start, 1);
$all .= $cut.$hash;
++$start;
--$count;
}
return $all;
}
echo code(1234);
?>
FYI it would have worked with your original code if you just used $all .= $cut . $hash as suggested which is exactly what I did.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum