需要帮助在PHP使用使preg_split()

问题描述:

任何人都可以解释如何使用使preg_split()函数来分割下面提到串需要帮助在PHP使用使preg_split()

String [ 

date=2017-05-31 time=14:12:05 devname=FGT3HD3914801291 devid=FGT3HD3914801449 logid=0316013056 type=utm subtype=webfilter eventtype=ftgd_blk level=warning vd="root" policyid=63 sessionid=9389050 user="" srcip=172.30.10.90 srcport=53542 srcintf="port5" dstip=50.7.146.50 dstport=80 dstintf="port2" proto=6 service=HTTP hostname="noblockweb.org" profile="IT ADMIN" action=blocked reqtype=direct url="/wpad.dat?1925450516382f9869bdfee527b429fb23737930" sentbyte=126 rcvdbyte=325 direction=outgoing msg="URL belongs to a denied category in policy" method=domain cat=55 catdesc="Meaningless Content" crscore=10 crlevel=medium 

] 

我需要在下面的输出结构 阵列 ( [0] =>日期2017年5月31日= [1] =>时间= 14:12:05 [20] => MSG = “URL属于在策略拒绝类别” 。。。。 )

preg_split可能不是正确的工具。您可以更好地使用preg_match_all进行这种拆分。

<?php 

    $str = 'date=2017-05-31 time=14:12:05 devname=FGT3HD3914801291 devid=FGT3HD3914801449 logid=0316013056 type=utm subtype=webfilter eventtype=ftgd_blk level=warning vd="root" policyid=63 sessionid=9389050 user="" srcip=172.30.10.90 srcport=53542 srcintf="port5" dstip=50.7.146.50 dstport=80 dstintf="port2" proto=6 service=HTTP hostname="noblockweb.org" profile="IT ADMIN" action=blocked reqtype=direct url="/wpad.dat?1925450516382f9869bdfee527b429fb23737930" sentbyte=126 rcvdbyte=325 direction=outgoing msg="URL belongs to a denied category in policy" method=domain cat=55 catdesc="Meaningless Content" crscore=10 crlevel=medium'; 

preg_match_all('/ ?(\w+\=(("[^"]*")|([^ ]*)))/',$str,$matches); 
print_r($matches[1]); 
  • '?' - 匹配一个空格字符,它被标记为可选的问号,因为它也应该匹配第一个项目。
  • 有没有找到匹配的部分,他们建立表达式块。这第一对paran是围绕我们感兴趣的部分。这就是为什么$matches[1]被使用。 $matches[0]包含整个匹配的部分,以及可能的第一个空格字符。
  • (... | ...) - 条形字符意味着它的左边或右边的部分可以匹配。
  • (“[^”]“) - 用于匹配引用的字符串。[^"]表示匹配所有不是引号的内容。方括号构建了一组匹配字符。如果脱字号是班级中的第一个字符,则表示该班级是倒立的。
  • ([^] *) - 所有东西都不是可能为零的空格字符。
+0

谢谢........ – jani