2012-04-20 164 views
1

我有注册表数据以文本如下:提取特定的PHP字符串基于关键字

/Classes/CLSID/AppID,SZ,{0010890e-8789-413c-adbc-48f5b511b3af}, 
/Classes/CLSID/InProcServer32,KEY,,2011-10-14 00:00:33 
/Classes/CLSID/InProcServer32/,EXPAND_SZ,%SystemRoot%\x5Csystem32\x5CSHELL32.dll, 
/Classes/CLSID/InProcServer32/ThreadingModel,SZ,Apartment, 
/Classes/CLSID/,KEY,,2011-10-14 00:00:36 
/Classes/CLSID/,SZ,, 
/Classes/CLSID/InprocServer32,KEY,,2011-10-14 00:00:36 
/Classes/CLSID/InprocServer32/,C:\x5CWINDOWS\x5Csystem32\x5Cmstime.dll, 

话,我$注册表=爆炸“\ n”和下面创建数组列表:

var_dump($registry); 

[1]=> string(121) "/Classes/CLSID/AppID,SZ,{0010890e-8789-413c-adbc-48f5b511b3af}," 
[2]=> string(139) "/Classes/CLSID/InProcServer32,KEY,,2011-10-14 00:00:33" 
[3]=> string(89) "/Classes/CLSID/InProcServer32/,EXPAND_SZ,%SystemRoot%\x5Csystem32\x5CSHELL32.dll," 
[4]=> string(103) "/Classes/CLSID/InProcServer32/ThreadingModel,SZ,Apartment," 
[5]=> string(103) "/Classes/CLSID/,KEY,,2011-10-14 00:00:36" 
[6]=> string(121) "/Classes/CLSID/,SZ,," 
[7]=> string(139) "/Classes/CLSID/InprocServer32,KEY,,2011-10-14 00:00:36" 
[8]=> string(89) "/Classes/CLSID/InprocServer32/,C:\x5CWINDOWS\x5Csystem32\x5Cmstime.dll," 

我也有关键字数组形式

var_dump($keywords); 

[1]=> string(12) "Math.dll" 
[2]=> string(12) "System.dll" 
[3]=> string(12) "inetc.dll" 
[4]=> string(12) "time.dll" 

我想表明在$注册表,在$关键字包括串线,所以我创建低于1个功能:

function separate($line) { 
     global $keywords; 
     foreach ($keywords as $data_filter) { 
      if (strpos($line, $data_filter) !== false) { 
     return true; 
      } 
     } 
     return false; 
    } 

$separate = array_filter($registry, 'separate'); 

因为在$关键字包括“time.dll”这样的代码产生的结果如下:

var_dump($seperate); 

[1]=> string(89) "/Classes/CLSID/InprocServer32/,C:\x5CWINDOWS\x5Csystem32\x5Cmstime.dll," 

在我的情况下,结果是不是因为,mstime真。 dll!= time.dll和信息不正确。

输出应该是空的。

可以说我替换为“\ x5C”作为空间,有什么功能可以完成这项工作吗?先谢谢你。

回答

2

preg_match

要随着你所要做的事情array_filter的路要走:

function separate($line) { 
    global $keywords; 
    foreach ($keywords as $data_filter) { 
     // '.' means any character in regex, while '\.' means literal period 
     $data_filter = str_replace('.', '\.', $data_filter); 
     if (preg_match("/\\x5C{$data_filter}/", $line)) { 
      return true; 
     } 
    } 
    return false; 
} 

如果你是这将返回false为

/Classes/CLSID/InprocServer32/,C:\x5CWINDOWS\x5Csystem32\x5Cmstime.dll, 

,但真正的

/Classes/CLSID/InprocServer32/,C:\x5CWINDOWS\x5Csystem32\x5Ctime.dll, 

不熟悉Regular Expressions,他们是真棒和强大。您可以根据需要定制我的地图以适应您的情况。

+0

生病请试试这个。感谢你的回答。 – Stream 2012-04-20 19:28:18