请问如何用php语言方法把下面的字符串按空格分割成数组,但是双引号里面的空格不分割。 10
42.236.50.235--[28/Oct/2016:03:26:14+0800]"GET/HTTP/1.1"20011112"http://www.huimeigou...
42.236.50.235 - - [28/Oct/2016:03:26:14 +0800] "GET / HTTP/1.1" 200 11112 "http://www.huimeigou.com/" "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 QIHU 360SE; 360Spider"
展开
2个回答
展开全部
<?php
$text = '42.236.50.235 - - [28/Oct/2016:03:26:14 +0800] "GET / HTTP/1.1" 200 11112 "http://www.huimeigou.com/" "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 QIHU 360SE; 360Spider"';
//$text = 'Lorem ipsum "dolor sit amet" consectetur "adipiscing "elit" dolor';
preg_match_all('/"(?:\.|[^"])*"|\S+/', $text, $matches);
echo "<pre>";
print_r($matches);
echo "</pre>";
我做了测试,有效!
追问
如果有多条同样格式的字符串信息,如何循环出来,我用下面的代码可是不行
$arr //数组为提问所示字符串数组
foreach($arr as $k=>$v){
preg_match_all('/"(?:\.|[^"])*"|\S+/', $arr[$k], $matchs);
$arr[$k]=$matchs[0];
unset($matchs);
}print_r($arr);
展开全部
$str = 42.236.50.235 - - [28/Oct/2016:03:26:14 +0800] "GET / HTTP/1.1" 200 11112 "http://www.huimeigou.com/" "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 QIHU 360SE; 360Spider";
preg_match_all('(([^\s"]+)|("[^"]+"))', $str, $out);
print_r($out[0]);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询