doq91130 2012-04-20 17:05
浏览 50
已采纳

根据关键字提取数组中的特定PHP字符串

i have registry data in text as below:

/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,

then i do $registry = explode " " and create list of arrays below:

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," 

i also have keywords in array form

var_dump($keywords);

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

i want to show lines in $registry that consist string in $keywords, so i create 1 function below:

    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');

since in $keywords consists "time.dll" so the codes produce result as below:

var_dump($seperate);

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

in my case the result is not true because, mstime.dll != time.dll and the information is improper.

the output should be empty.

lets say i replace the "\x5C" as space, there is any function that can do the job? thank you in advance.

  • 写回答

1条回答 默认 最新

  • doujiao4705 2012-04-20 17:12
    关注

    There's preg_match.

    To go along with the array_filter way you have to do things:

    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;
    }
    

    This would return false for

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

    but true for

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

    If you're not familiar with Regular Expressions, they are awesome and powerful. You can customize mine as needed to suit your situation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?