views:

35

answers:

1

I have a string of registry keys that looks like this:

ThreatName REG_SZ c:\temp Protection Code REG_SZ a Check ThreatName REG_SZ c:\windows Protection

I want to extract "c:\WHATEVER" from the string. It occurs multiple times between the words "ThreatName REG_SZ" and "Protection".

How can I extract "c:\WHATEVER" multiple times using PHP?

+1  A: 

One way to do it is using Regular Expressions, here is an example code (un-tested).


$string = "ThreatName REG_SZ c:\\temp Protection
Code REG_SZ c:\\a Check
ThreatName REG_SZ c:\\windows Protection";

preg_match_all("~.* REG_SZ (.*) ~iU", $string, $matches);

print_r($matches);

If you want to understand more see the php manual for: preg_match_all(). Or google regular expressions for more information on them. But basically it looks between the REG_SZ and Protection (the U modifier makes it ungreedy so it will look for the first Protection) and returns everything but the new line character (the .*). If this is spread across new lines, the 's' modifier will help resolve that.

EDIT: Saw that you wanted them all. This should work for all of them.

EDIT: Fixed the regex to include "ThreatName", not sure if this is dynamic. Also added extra slashes to the string as they were being parsed as characters.

I am not sure if you will have to use addslashes() on the string or not, but it maybe needed.

Removed the isset as it was not necessary.

EDIT: Modified the code given that the correct output formatting was omitted. The updated method will work, but if the directory has a space in it, chances are it will only pull the first part of the directory.

Brad F Jacobs
Hmm....that code doesn't work for me. It doesn't echo anything.
ThisIsMyUsername
Modified it a bit. I forgot about windows slashes acting as escape \ special characters. IE \t = tab. You may need to utilize addslashes, may not. But the modified code above should do what you want and will perhaps at least give you a working example so you can modify your code accordingly.
Brad F Jacobs
It is enough to use `if (!empty($matches[1])) {}`; `empty($var)` returns `TRUE` for a variable set to `NULL`, or that has not been initialized.
kiamlaluno
Yep, you are right. I will modify accordingly, I thought it would throw a "notice" error for not being defined.
Brad F Jacobs
EDIT: The code works fine when I write the string in the PHP file.Example: $string = "huge string to parse";That works.However, when I use your code with the string as a variable (not defined in the file ($string = shell_exec('file.bat '.$arg);)I can print out $string but your code cannot parse it.Any ideas?
ThisIsMyUsername
Chances are when it is printing out it has extra characters to it that were not present when you posted it here. If you are doing this via a web browser, view the source and copy / paste that output in `<pre></pre>` tags (you should be able to edit your original post) in order to get a better idea of what the output contains and how to parse it properly.
Brad F Jacobs
OK, you are right about the formatting. The source shows:ThreatName REG_SZ C:\osSomethingElse REG_SZ C:\fileHow can I pull out the THREATNAME line from the formatting?
ThisIsMyUsername
I lost my formatting but imagine line breaks after C:\whatever
ThisIsMyUsername
Modified the code. You should still be able to re-generate the output and copy it into `<pre></pre>` tags (via an edit to your original post) and that will show the proper formatting of it being returned. Without that proper formatting (given that the new modification does not work) we are just grasping at straws here and wasting time.
Brad F Jacobs
How can I just extract all the lines with "DirectoryName" as the start?
ThisIsMyUsername