Home > Mobile >  show only 2 digit and 2 digit in output
show only 2 digit and 2 digit in output

Time:09-10

I'm doing a particular exercise on a deserialized string in PHP. In practice, this string once serialized-deserialized by the webapp takes and joins the two fields 'id' and 'uid' and then adds them together to form a single 4-digit field.

example:

O: 8: "userdata": 3: {s: 4: "role"; s: 5:"admin"; s: 2: "id"; i: 21; s: 3: "uid"; i: 32 ;}

the webapp will give me the result: welcome admin 2132

I need to create an incremental numeric loop in order to have the id and uid field 2 cyclic digits that is:

O: 8: "userdata": 3: {s: 4: "role"; s: 5: "admin"; s: 2: "id"; i: 00; s: 3: "uid"; i: 00 ;}
O: 8: "userdata": 3: {s: 4: "role"; s: 5: "admin"; s: 2: "id"; i: 00; s: 3: "uid"; i: 01 ;}

and so on, until the uid field reaches 99 and the id field starts adding 1 and becoming 01 while the uid field must always do the same cycle.

i mean this:

O: 8: "userdata": 3: {s: 4: "role"; s: 5: "admin"; s: 2: "id"; i: 00; s: 3: "uid"; i: 99 ;}
O: 8: "userdata": 3: {s: 4: "role"; s: 5: "admin"; s: 2: "id"; i: 01; s: 3: "uid"; i: 00 ;}
O: 8: "userdata": 3: {s: 4: "role"; s: 5: "admin"; s: 2: "id"; i: 01; s: 3: "uid"; i: 01 ;}
O: 8: "userdata": 3: {s: 4: "role"; s: 5: "admin"; s: 2: "id"; i: 01; s: 3: "uid"; i: 02 ;}

I created this script so that I have 4 incremental digits but the problem is that I can't declare 2 digits in the id field and another 2 digits in the uid field in order to have the result I need.

<?php
for($a=0; $a< 10; $a  )
 { 
   for($b=0; $b< 10; $b  )
      {
          for($c=0; $c< 10; $c  ) {
            for($d=0; $d< 10; $d  ) {

              $y = ($a . $b. $c. $d);
              $plain = 'O:8:"userdata":3:{s:4:"role";s:5:"admin";s:2:"id";i:'.$y.';s:3:"uid";i:'.$y.';}';
              echo $plain. "\n";
            }
          }
          
      }
 }
?>

The output of these for loops is nothing more than:

O:8:"userdata":3{s:4:"role";s:5:"admin";s:2:"id";i:0000;s:3:"uid";i:0000;}
O:8:"userdata":3{s:4:"role";s:5:"admin";s:2:"id";i:0001;s:3:"uid";i:0001;}
O:8:"userdata":3{s:4:"role";s:5:"admin";s:2:"id";i:0002;s:3:"uid";i:0002;}
O:8:"userdata":3{s:4:"role";s:5:"admin";s:2:"id";i:0003;s:3:"uid";i:0003;}
O:8:"userdata":3{s:4:"role";s:5:"admin";s:2:"id";i:0004;s:3:"uid";i:0004;}

CodePudding user response:

Why don't you just count from 0000 to 9999, splitting to id and uid to have each 2 digits?


for ($i = 0; $i < 1e4; $i  ) {
    $str = "0000$i";
    $len = strlen($str);
    $id = substr($str, $len-4, 2);
    $uid = substr($str, $len-2, 2);
    echo "$id $uid<br>";
}

CodePudding user response:

Thank you @IT goldman I solved with:

<?php

for ($i = 0; $i < 1e4; $i  ) {
    $str = "0000$i";
    $len = strlen($str);
    $id = substr($str, $len-4, 2);
    $uid = substr($str, $len-2, 2);
   
    $string = 'O:8:"userdata":3:{s:4:"role";s:5:"admin";s:2:"id";i:'.$id.';s:3:"uid";i:'.$uid.';}';
    echo $string."\n";
}

?>
  • Related