且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP爆炸功能

更新时间:2023-12-04 13:44:16

只要引号内不能有引号(例如, foo bar 是不允许的),则可以使用正则表达式执行此操作,否则需要一个完整解析器。

As long as there can't be quotes within quotes (eg. "foo\"bar" isn't allowed), you can do this with a regular expression. Otherwise you need a full parser.

应该这样做:

function split_words($input) {
  $matches = array();
  if (preg_match_all('/("([^"]+)")|(\w+)/', $input, $reg)) {
    for ($ii=0,$cc=count($reg[0]); $ii < $cc; ++$ii) {
      $matches[] = $reg[2][$ii] ? $reg[2][$ii] : $reg[3][$ii];
    }
  }
  return $matches;
}

用法:

$input = 'windows linux "mac os x"';
var_dump(split_words($input));