How to manipulate the string coming from a . reg file exported from regedit?

Asked

Viewed 212 times

1

How to abstract the values of these individual parameters and store in a variable, or use to compare with another variable ?

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\leroLero]
"parametro1"=valor
"parametro2"=valor
"parametro3"=valor
  • 2

    It is a question about PHP itself, want to take the data of . reg and read in a php script? PS: the downvote is not mine.

  • yes read in PHP and compare in script . with other variables .

  • 1

    strange every time I ask question instead of help put negative .....

  • 1

    Maybe they were negative because the way the question is a little difficult to understand, I myself had this difficulty at first, there are people who are easy to communicate verbally, but can not do also via internet.

  • It wants to parse data from a file exported from the windows registry.. the origin of all this came from this post: https://answall.com/questions/226338 and has another very confusing

  • 1

    If it’s an ini file you can start with: How to transform an INI file into an Array?

  • is . reg the file !

Show 2 more comments

2 answers

2

I believe the format is quite similar to that of the .ini (of course it has variations, but this is another situation, for the code above I believe it is similar), if it really is similar you can use the parse_ini_string, of course before it is necessary to make use preg_replace to remove some things, on specific case I just needed to remove the key quotes, example:

<?php
$regfile = file_get_contents('arquivo.reg');

$regfile = preg_replace('#(\r|\n)"|"(=)#', '$1$2', $regfile);

$parsed = parse_ini_string($regfile, false, INI_SCANNER_RAW);

var_dump($parsed);

An online example: https://ideone.com/ZyHnS0

<?php

$reg = 'Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\leroLero]
"parametro1"=valor
"parametro2"=valor
"parametro3"=valor';

$reg = preg_replace('#(\r|\n)"|"(=)#', '$1$2', $reg);

$parsed = parse_ini_string($reg, true, INI_SCANNER_RAW);

var_dump($parsed);

Will return something like:

Array
(
    [parametro1] => valor
    [parametro2] => valor
    [parametro3] => valor
)

So just compare it like this:

if ($parsed['parametro1'] == 'valor') {
   echo 'algo aqui';
}

Note: in case I did not process the sessions, this as false:

 $parsed = parse_ini_string($reg, true, INI_SCANNER_RAW);

but can process using true if there are more "sessions":

$parsed = parse_ini_string($reg, true, INI_SCANNER_RAW);

Then the result will be: https://ideone.com/wqyzXu

Array
(
    [HKEY_LOCAL_MACHINE\SYSTEM\leroLero] => Array
        (
            [parametro1] => valor
            [parametro2] => valor
            [parametro3] => valor
        )

)

1


The idea is to simplify by going straight to the point, making an explode() grosserio to transform each line into an array item.

Then read the relevant index.

Since the first two are mere noises, skip straight to index 3 where the first parameter is:

$data = 'Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\leroLero]
"parametro1"=valor
"parametro2"=valor
"parametro3"=valor';

$arr = explode('
', $data);

// Vai direto no índice 3, mas antes faz uma limpeza, removendo as aspas duplas.
// A função parse_str() auxilia na conversão do formato de query para variáveis
$val = parse_str(str_replace('"', '', $arr[3]), $param);

// Agora pode acessar de forma amigável o parâmetro que necessita.
echo $param['parametro1'];

If you need to read the parametro2, position is 4 (line number) and at the time of reading just call echo $param['parametro2'];.

Of course this is a didactic example. It does no validation if the index exists, etc. It is good to create consistency in the script to avoid problems.

  • Thank you Daniel Omine , I’ll adapt everything here .

  • OK. Thank you , Daniel Omine , for your help. THANK YOU.

Browser other questions tagged

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