在PHP中将字符串拆分为具有各种分隔符的数组


Split string into array with various delimiters in PHP

我想知道如何使用regex将其拆分为数组:

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

您可以尝试并调整如下内容:

$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/>';
}

注意:您可以将所需的所有字段放在一个数组中,并减少代码的大小。

问题不在于您试图将字符串拆分为具有各种分隔符的数组。

您的问题是,您正试图从用户代理字符串中进行浏览器检测。

对于你遇到的每一个编程问题,都要问问自己:"这是其他人可能已经遇到的问题吗?我可能会利用他们的解决方案?"

如果是这样,那就试着在谷歌上搜索答案。在这种情况下,我在谷歌上搜索了"php解析用户代理"。那次搜索把我带到了StackOverflow上的这个页面,它让我找到了这个内置于PHP本身的函数。