1

I have a website (site-1) which import pages from other website (site-2).

These pages have an id number in site-2 and this number is copied to site-1 when the importing is done. So far so good.

Problem is that the id of site-2 are huge, e.g.: 32423201639, 3212450421639,... and the sistem in site-1 is no able to handle them. So I need to make these numbers smaller when the importing is done.

It is important:

  • to generate unique number values.

  • to be bigger than 3000 and smaller than 10000.

  • It can not use rand(). If we execute this several time the results must be the same

UPDATE: To keep in mind:

This importing is done every week, so I need to consider this: Let's say a first importing is done, and then in the second importing ONLY the first array value changes but the others remain then this one will be the only one to be changed and the other will keep the same value as in the first importing.

The first thing I thought was something like this (but the most important is missing):

$array_values_site1 = array("12345" , "123456", "1234567", "12345678", "123456789", "1234567890", "12345678901", "123456789012", "1234567890123", "12345678901234", "123456789012345", "1234567890123456");

$array_values_site2 = array();
foreach ($array_values_site1 as &$value) {


    /* here I need to change the value of $value:
    --- to be bigger than 3000 and smaller than 10000.
    --- It can not use rand(). If we execute this several time the results must be the same
    --- to be unique */ 
    $new_value = "....";

    $array_values_site2 [] = $new_value;
}
14
  • Does PHP have a rand which takes a seed to produce repeatable results? Commented Feb 3, 2016 at 15:07
  • @BillWoodger not sure but I think that rand() will generate a new value every time you run it. Commented Feb 3, 2016 at 15:10
  • 1
    There a reason to not use simple incremental values 3001,3002,3003, etc...? Also, you will stored array values (in db or file) or you use they only at runtime? Commented Feb 3, 2016 at 15:12
  • Why not just set up a primary key on the new table starting the auto increment value at 3000? ALTER TABLE users AUTO_INCREMENT=3000; Commented Feb 3, 2016 at 15:17
  • @fusion3k yes there is. Let's say a first importing is done, and then in the secondo importing the first array value changes but the others remain the same then your approach will fail. This is an important point, I will add it to the question. Commented Feb 3, 2016 at 15:18

1 Answer 1

1

Looking at the comments, hashing the original ID appears best:

$hashids = new Hashids\Hashids('this is my salt');
$id = $hashids->encode(1);
$original = $hashids->decode($id);

To specify a minimum length (not a number, but a length) and characters to use in the result, include a second and third parameter:

$hashids = new Hashids\Hashids('this is my salt');
$id = $hashids->encode(1, 8, 'abcdefghij1234567890');
$original = $hashids->decode($id);
// $id = '514cdi42';

See hashids.org and github for info.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.