我想知道如何使用正则表达式将其拆分为数组:
input = "1254033577 2009-09-27 06:39:37 "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1" 44.12.96.2 Duncan OK US Hot Buys http://www.esshopzilla.com/hotbuys/ http://www.google.com/search?hl=en&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=Zk5&q=ipod&aq=f&oq=&aqi=g-p1g9"
array (
1254033577,
2009-09-27 06:39:37,
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1, 44.12.96.2,
Duncan,
OK,
US,
Hot Buys,
http://www.esshopzilla.com/hotbuys/,
http://www.google.com/search?hl=en&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=Zk5&q=ipod&aq=f&oq=&aqi=g-p1g9"
)
答案 0 :(得分:3)
你可以尝试和改编这样的东西:
$pattern = '~(?<id>\d++)' . '\s++'
. '(?<datetime>\d{4}-\d{2}-\d{2}\s++\d{2}:\d{2}:\d{2})' . '\s++"'
. '(?<useragent>[^"]++)' . '"\s++'
. '(?<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' . '\s++'
. '(?<name>\S++)' . '\s++'
. '(?<response>[A-Z]++)' . '\s++'
. '(?<country>[A-Z]{2,3})' . '\s++'
. '(?<title>(?>[^h\s]++|\s*+(?>h(?!ttp://))?|\s++)+)' . '\s++'
. '(?<url>\S++)' . '\s++'
. '(?<search>\S++)~';
preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
foreach($matches as $match) {
echo '<br/>id: ' . $match['id'] . '<br/>datetime: ' . $match['datetime']
. '<br/>user agent: ' . $match['useragent'] . '<br/>ip: ' . $match['ip']
. '<br/>name: ' . $match['name'] . '<br/>response: ' . $match['response']
. '<br/>country: ' . $match['country'] . '<br/>title: ' . $match['title']
. '<br/>url: ' . $match['url'] . '<br/>search: ' . $match['search']
. '<br/>';
}
注意:您可以将所需的所有字段放在数组中,并减小代码的大小。
答案 1 :(得分:0)
您的问题不在于您是否尝试将字符串拆分为具有各种分隔符的数组。
您的问题是您正尝试从用户代理字符串进行浏览器检测。
对于您遇到的每一个编程问题,请问自己“这是其他人可能已经拥有的东西,我可以利用他们的解决方案吗?”
如果是这样,那么尝试谷歌搜索答案。在这种情况下,我用Google搜索“php解析用户代理”。那次搜索让我this page on StackOverflow导致我this function that is built in to PHP itself。