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 0x1开发者_运维知识库1
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?
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
Something like this should work...
$pattern = '#\*\*DirectoryName\s+REG_SZ\s+(.*?)\*\*#';
if (preg_match($pattern, $input, $output)) {
print_r($output);
}
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
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):
- http://www.phpclasses.org/package/1426-PHP-Access-to-the-Windows-Registry.html
精彩评论