此正则表达式将帮助您:
$result=array();
$subject = 'TEXT "PLAIN" (string with spaces) "string with other spaces" ("NAME" "file(1).txt") NIL';
$regex = '
/"([^"])+" # Match quote, followed by multiple non-quotes, ended by a quote.
|(\([\w ]+\)) # Or match multiple words and spaces between parentheses
|\(((?=")([^)]|(?>"|.))+)\) # Or Match text between parentheses, ignore ending parenthese if inside a quote
|\w+ # Or match single words
/x';
preg_match_all($regex, $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];
print_r($result);
print_r($result);
测试字符串:
TEXT "PLAIN" (string with spaces) "string with other spaces" ("NAME" "file(1).txt") NIL
结果 :
Array
(
[0] => TEXT
[1] => "PLAIN"
[2] => (string with spaces)
[3] => "string with other spaces"
[4] => ("NAME" "file(1).txt")
[5] => NIL
)