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 ; } PHP: 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; PHP: so please how can i make the function returns with the full number $cut.$hash (1xx2xx3xx4xx) ?? thanks
<?php function code($num) { $count = strlen($num); $start = "0"; while($count > 0) { $rand = rand(0,15); $hash = substr(md5(rand(0,999)), $rand, 2); $cut = substr($num, $start, 1); $all .= $cut.$hash; ++$start; --$count; } return $all; } echo code("1234") ; ?> PHP: output : 11423530547a had to move stuff around to make it print different numbers for filler. Aint sure if its what you want.
one more question please . when i was trying to got my main number back again , i wan using that function function code($num) { $count = strlen($num); $number = $count/3; $first = "0"; while($number > 0) { $cut = substr($num, $first, 1); $back= $cut; $first = $first+3; --$number; } return $back; } echo code("1xx2xx3xx4xx") ; PHP: but it have the same error like the previous one , so how can i fix it to make it returns the full number "1234" back ??? thanks
Let's see : our numbers come in groups of 3 we take the first character from a group of 3. --snip--- here I tested this to work, isn't function but oh well... <?php function code($num) { $count = strlen($num); $start = "0"; while($count > 0) { $rand = rand(0,15); $hash = substr(md5(rand(0,999)), $rand, 2); $cut = substr($num, $start, 1); $all .= $cut.$hash; ++$start; --$count; } return $all; } $out_num = code("1234") ; $out_count = strlen($out_num); $out_3 = $out_count / 3 ; for($i=0;$i<$out_3;$i++) { $start_pos = $i * 3 ; if($i == 0) { $start_pos = 0; } $str = substr($out_num , $start_pos, 1) ; echo $str ; } ?> PHP:
thanks you very much for the code and this helpful replies . but i guess your for code have the same error like my while code , their only display the data between the while and for tags , but if we tried to display the data in variable outside the for tags it not gonna work $out_num = "1xx2xx3xx4xx" ; $out_count = strlen($out_num); $out_3 = $out_count / 3 ; for($i=0;$i<$out_3;$i++) { $start_pos = $i * 3 ; if($i == 0) { $start_pos = 0; } $str = substr($out_num , $start_pos, 1) ; } echo $str ; PHP: it returns the last number only "4" , not the full number "1234" as we looking for. so please any other suggestion to return the full number back in variable outside the while or the for tags ??? thanks a lot