Gget 潜在嵌套大括号内的所有子字符串
我正在尝试用 PHP 解析以下格式:
// This is a comment
{
this is an entry
}
{
this is another entry
}
{
entry
{entry within entry}
{entry within entry}
}
也许只是缺少咖啡因,但我想不出一种获取大括号内容的好方法。
I'm trying to parse the following format with PHP:
// This is a comment
{
this is an entry
}
{
this is another entry
}
{
entry
{entry within entry}
{entry within entry}
}
Maybe is just the lack of caffeine, but I can't think of a decent way of getting the contents of the curly braces.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个非常常见的解析任务,基本上您需要跟踪可能处于的各种状态,并使用常量和函数调用的组合来维护它们。
下面是一些相当不优雅的代码,它就是这样做的:
这会产生以下输出:
您可能想要清理所有空白,但一些放置良好的修剪会为您进行排序。
This is quite a common parsing task, basically you need to keep track of the various states you can be in and use a combination of constants and function calls to maintain them.
Here is some rather inelegant code that does just that:
This produces the following output:
You'll probably want to clean up all the whitespace but some well placed trim's will sort that for you.
对于大量内容来说,这可能不是最佳解决方案,但它确实有效。
结果:
This may not be the best solution for large amount of content, but it works.
Results: