tags:

views:

59

answers:

4

I need to extract "C:\Documents and Settings" from the last line of this data:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft
ExcludeSubDirs   REG_DWORD 0x1
ExtensionList  REG_SZ 
FirstAction          REG_DWORD 0x11
ThreatName          REG_SZ         C:\Documents and Settings
Owner          REG_DWORD 0x3
ProtectionTechnolog  REG_DWORD 0x1
SecondAction  REG_DWORD 0x11
DirectoryName  REG_SZ         C:\Documents and Settings

How can I extract "C:\Documents and Settings" or whatever the value is multiple times using PHP?

A: 

Use regular expressions

$str = 'your string';
preg_match_all('!HKEY.+?DirectoryName\s+REG_SZ\s+([^\n]+)!s', $str."\nHKEY", $matches);
$dirs = @array_map('trim', $matches[1]); 

Your matches will be in the $dirs array.
Here is a working sample: http://ideone.com/rdTOx

NullUserException
The two asterisks are actually supposed to bold the statement.How can I do it without the asterisks?
David Lock
@David It won't change much
NullUserException
Perfect! Thanks.
David Lock
A: 

Something like this should work...

$pattern = '#\*\*DirectoryName\s+REG_SZ\s+(.*?)\*\*#';
if (preg_match($pattern, $input, $output)) {
   print_r($output);
}
Cags
I think what you want is a positive lookbehind. This will check that Directory Name REG_SZ exists before the matched text but not return it in the match
Keyo
A: 

I found this on google.

Just put the line containing "DirectoryName" into the function.

preg_match('DirectoryName.*', $str, $matches);
$directory_line = $matches[0];

http://samburney.com/blog/regular-expression-match-windows-or-unix-path

Keyo
A: 

If this is on Windows, you can access the value directly through the COM interface:

$WshShell = new COM("WScript.Shell");
$result = $WshShell->RegRead(
    'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DirectoryName');

echo $result; // C:\Documents and Settings

The above assumes there really is a key "DirectoryName" at the given position.

And there is also this class (cant tell if it's any good):

Gordon
Right, but I need to get multiple DirectoryName, not just one.
David Lock
@David not sure I understand. Just change the key.
Gordon
@Gordon I think he needs to get a whole bunch of them.
NullUserException
@NullUser and why cant he fetch the whole bunch? :)
Gordon
@Gordon I am guessing that he doesn't have list with all of them.
NullUserException
@NullUser well, maybe. Anyway, it's not like I'm insisting he does it with COM. Just trying to give alternatives to what was already suggested.
Gordon