help me with that function please

Discussion in 'PHP' started by crazy.works, Mar 10, 2009.

  1. #1
    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
     
    crazy.works, Mar 10, 2009 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    <?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.
     
    shallowink, Mar 10, 2009 IP
  3. crazy.works

    crazy.works Peon

    Messages:
    304
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks a lot it is exactly the fix which i was trying to do :)
     
    crazy.works, Mar 10, 2009 IP
  4. crazy.works

    crazy.works Peon

    Messages:
    304
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    crazy.works, Mar 10, 2009 IP
  5. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #5
    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:
     
    shallowink, Mar 10, 2009 IP
  6. crazy.works

    crazy.works Peon

    Messages:
    304
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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
     
    crazy.works, Mar 10, 2009 IP
  7. crazy.works

    crazy.works Peon

    Messages:
    304
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thanks fixed
    
     $str .= substr($out_num , $start_pos, 1) ; 
    
    PHP:
    thanks again :)
     
    crazy.works, Mar 10, 2009 IP