【问题标题】:How to split code into tokens using php?如何使用 php 将代码拆分为令牌?
【发布时间】:2016-05-27 13:15:20
【问题描述】:

我的字符串中的代码如下所示:

$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"]

&lt;html&gt;&lt;body&gt; 不见了,我的代码有什么问题?

【问题讨论】:

    标签: php split tokenize


    【解决方案1】:

    页面是 html,它 &lt;html&gt;&lt;body&gt; 被解释为 html,所以它没有显示。设置 content-type 解决了这个问题:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多