如何使用 php 将代码拆分为令牌?

How to split code into tokens using php?

我在字符串中有这样的代码:

$code = "<html>
<body>
(if(foo == 10 || (foo == 20 && bar == 30))
    (foo(something)foo)
)if)
</body>
</html>";

如何拆分字符串以获得结果:

array(
 "<html>\n<body>",
 "(if(",
 "foo == 10 || (foo == 20 && bar == 30))"
 "    ",
 "(foo(",
 "something",
 ")foo)",
 ")if)",
 "</body>\n</html>"
);

到目前为止我有这个:

$tokens = preg_split("/(\(\w+\(|\)\w+\))/", $code, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
echo json_encode($tokens);

但作为回应,我得到了这个:

["\r\n\r\n\r\n","(if(","foo == 10 || (foo == 20 && bar == 30))\r\n ","(foo(","something",")foo)","\r\n",")if)","\r\n<\/body>\r\n<\/html>\r\n"]

<html><body> 不见了,我的代码有什么问题?

该页面是 html,它 <html><body> 被解释为 html,所以它没有显示。设置内容类型解决了问题:

header('Content-Type: text/plain');