Scroll through a.txt file and extract a "PK" from the file

Asked

Viewed 103 times

0

I started recently with programming in PHP and I’m having some difficulty in understanding some functions anyway, I’m participating in a project, and as part of the project I need to store a array in a arquivo.txt that I person read later(up to here blz, saved the file with file_input_contents, and used the serialize function so that it is possible to write the array inside a file).

My difficulty is:

1) How do I read the file? A: duties fopen()? Blz, how do I go through this file and remove only one string, exactly one PK of that file?

$conteudo = serialize($array);      
$caminho ='';
file_put_contents($caminho, $conteudo);

Content of array barring:

a:1:{s:5:"items";a:1:{i:0;a:1:{s:2:"pk";i:1338838365890273563;}}}

  • I don’t know if it’s clear, but I want to get the code after i: which would be 13388385890273563. Vlw!!!!

  • you get exactly that a:1:{s:5:"items";a:1:{i:0;a:1:{s:2:"pk";i:1338838365890273563;}}} ?

  • 1

    Voce can use the file_get_contents to read the file and unserialize for the text to be array again

  • 2

    Do exactly the reverse so that using file get Contents and then unserialize, will have an array with the same data as before... (Sorry, I’m on mobile and can’t post as a decent response)

1 answer

0

If it’s just to extract the number from the string, you can do just that:

$value =  'a:1:{s:5:"items";a:1:{i:0;a:1:{s:2:"pk";i:1338838365890273563;}}}';

preg_match('/"pk"\;i\:([0-9]+)\;/', $value, $d);

echo $d[1];

Now if you want to review the array value, you can revert the serialize this way:

$conteudo_salvo = 'a:1:{s:5:"items";a:1:{i:0;a:1:{s:2:"pk";i:1338838365890273563;}}}';
echo unserialize($conteudo_salvo)['items'][0]['pk'];

However, there is one problem that can modify the conversion behavior, is that the serialized number is converted to scientific notation, that is, lost the complete reference of the integer:

 $a = array(
         'items' => array( 
                     0 => array( 'pk' => 1338838365890273563
                          ) 
                   )
    );

On the way out of unserialize, it will remain as scientific notation, because the value is too great to be serialized: 1.3388383658903E+18
How to make the serialize be done correctly, pass it in string format:

$a = array(
         'items' => array( 
                     0 => array( 'pk' => "1338838365890273563"
                          ) 
                   )
    ); 

Doing this, just use the functions in this way:

$s = serialize($a);
$u = unserialize($s);

print_r($a);
print_r($u);

OBS: Three simple ways to transform the integer value to string:

$a["items"][0]["pk"] = '"' . $a["items"][0]["pk"] . '"'; 

or

$a["items"][0]["pk"] = (string) $a["items"][0]["pk"];

or

$a["items"][0]["pk"] = strval($a["items"][0]["pk"]);
  • Thanks man, it helped a lot. vlw, hug!

  • Good afternoon, instead of 'serialize' I chose to use json_encode($array) to have an easier format to access, because the data will be stored in the database. The data from the json_encode array was: {"items":[{"pk":1338838365890273563}]}. To get the "PK" I use this same logic?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.