Jump to content

xyph

Staff Alumni
  • Posts

    3,711
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by xyph

  1. Alternately,

     

    <?php
    
    $values = array(
    // Number => Weighted chance
    1 => 1,
    2 => 1,
    3 => 5, // 5x more likely than 1,2
    4 => 3  // 3x more likely than 1,2
    );
    
    // The total of all the chances
    $total = array_sum($values);
    // Will track the sum of all tested chances
    $i = 0;
    // The number we're looking to find
    $find = mt_rand(1,$total);
    // Loop through value=>chance array
    foreach( $values as $val => $chance ) {
    // Add current chance to the running total
    $i += $chance;
    // Check if the running total equals or exceeds the number we're trying to find
    if( $find <= $i )
    // If so, break out of the loop
    break;
    }
    // The last assigned value before breaking will contain the number
    echo $val;
    
    ?>

     

    The process is more complex, but the input is more simplified.

  2. Force everyone to use OSX?

     

    Expecting a browser to render fonts like a design application is just silly :) I believe most just piggy-back on the OS' rendering engine.

     

    Use images, static or dynamic, if you need fonts to render in a certain way.

  3. listing them individually is the easiest way, but at this point you're allowing most of the ascii range. Might as well make a blacklist, or specify your ranged by hand using an ascii chart.

     

    If you use an array as a string, it becomes the word "Array." Implode turns an array into a string so you can actually see the values.

     

    I think it's preg_quote that's messing up the ranges (\-).

     

    IMO, using a blacklist is a bad idea unless you're limiting all input to the ASCII range, and even then it would be pretty big.

     

    A solution could be to keep ranges in a separate array, and not preg_quote those.

  4. Assuming the algorithm is well built - the shorter the digest, the greater chance of collision.

     

    The data returned in a digest is always binary, just generally in hex form. In this case, a 32-bit integer is returned. They have quite a bit written in the big, red warning box in the manual entry - including how to convert it to hex.

     

    http://php.net/manua...ction.crc32.php

     

    Keep in mind, a 10-digit integer can be 'shorter' than a 5 character string, in a storage sense. Hex is generally an inefficient way to store binary data (assuming it's a string)

  5. I think he wants the token to stay consistent over multiple requests.

     

    I don't think using the session ID is a good idea, even if obfuscated or hashed.

     

    Your best bet is to generate a string based on random data, then store this in a cookie with a FALSE expiration, so it expires with the user.

     

    We'd need more details to give you a better solution

  6. Where are you getting $reference_points.[2]=9

     

    There's no 9 in 5.2.80.0/21 5.11.128.0/17

     

    You probably want this

     

    <?php
    
    $string = '5.2.80.0/21 5.11.128.0/17';
    
    $pattern = '%(\d+)\.(\d+)\.(\d+)\.(\d+)/(\d+)%';
    preg_match_all($pattern, $string, $matches, PREG_SET_ORDER);
    
    var_dump($matches);
    
    ?>

     

    Output

     

    array (size=2)
      0 => 
        array (size=6)
          0 => string '5.2.80.0/21' (length=11)
          1 => string '5' (length=1)
          2 => string '2' (length=1)
          3 => string '80' (length=2)
          4 => string '0' (length=1)
          5 => string '21' (length=2)
      1 => 
        array (size=6)
          0 => string '5.11.128.0/17' (length=13)
          1 => string '5' (length=1)
          2 => string '11' (length=2)
          3 => string '128' (length=3)
          4 => string '0' (length=1)
          5 => string '17' (length=2)

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.