David Lock
David Lock

Reputation: 1

PHP Extract Line

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?

Upvotes: 0

Views: 125

Answers (4)

NullUserException
NullUserException

Reputation: 85458

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

Upvotes: 1

Gordon
Gordon

Reputation: 316969

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):

Upvotes: 0

Benbob
Benbob

Reputation: 14244

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

Upvotes: 0

Peter O'Callaghan
Peter O'Callaghan

Reputation: 6186

Something like this should work...

$pattern = '#\*\*DirectoryName\s+REG_SZ\s+(.*?)\*\*#';
if (preg_match($pattern, $input, $output)) {
   print_r($output);
}

Upvotes: 0

Related Questions