如何在 PHP 中首先处理最大的匹配?
好的,所以我首先尝试四处搜索,但我不知道如何表达这个问题或搜索短语。让我解释一下。
我的数据看起来像这样:
<!-- data:start -->
<!-- 0:start -->
<!-- 0:start -->0,9<!-- 0:stop -->
<!-- 1:start -->0,0<!-- 1:stop -->
<!-- 2:start -->9,0<!-- 2:stop -->
<!-- 3:start -->9,9<!-- 3:stop -->
<!-- 4:start -->0,9<!-- 4:stop -->
<!-- 0:stop -->
<!-- 1:start -->
<!-- 0:start -->1,5<!-- 0:stop -->
<!-- 1:start -->1,6<!-- 1:stop -->
<!-- 2:start -->3,6<!-- 2:stop -->
<!-- 3:start -->3,8<!-- 3:stop -->
<!-- 4:start -->4,8<!-- 4:stop -->
<!-- 1:stop -->
<!-- 2:start -->
<!-- 0:start -->0,7<!-- 0:stop -->
<!-- 1:start -->1,7<!-- 1:stop -->
<!-- 2:stop -->
<!-- data:stop -->
所以它基本上是一堆点。这是我当前用来尝试解析它的代码,以便它创建一个如下所示的数组:
Array (
0 => Array (
0 => "0,9",
1 => "0,0",
2 => "9,0",
3 => "9,9",
4 => "0,9"
),
1 => Array (
0 => "1,5",
1 => "1,6",
2 => "3,6",
3 => "3,8",
4 => "4,8"
),
2 => Array (
0 => "0,7",
1 => "1,7"
)
)
但是,它返回一个如下所示的数组:
Array (
0 => "0,9",
1 => "0,0",
2 => "9,0"
)
查看屏幕上的较大数组,您会看到它在匹配时设置该变量的第一个实例。那么我如何让它首先找到最广泛的匹配,然后处理内部。这是我当前正在使用的功能:
function explosion($text) {
$number = preg_match_all("/(<!-- ([\w]+):start -->)\n?(.*?)\n?(<!-- \\2:stop -->)/s", $text, $matches, PREG_SET_ORDER);
if ($number == 0) return $text;
else unset($item);
foreach ($matches as $item) if (empty($data[$item[2]])) $data[$item[2]] = $this->explosion($item[3]);
return $data;
}
我确信这将是我忽略的一些愚蠢而简单的功能,但我想这对您来说只是一个简单的答案。
编辑:这是我的整个数据集的完整输出日志这个样本取自。标签被打印出来(用 > 和 < 替换它们),并且全部位于一个巨大的
元素中,以便于阅读。
这是混乱的部分:
Array ( [0] => <!-- 0:start --> <!-- 0:start -->0,9<!-- 0:stop --> [1] => 0 [2] => <!-- 0:start -->0,9 )
0 => <!-- 0:start -->0,9
所以它在第一次出现其中另一部分的停止标签时停止。我是否应该考虑相反的方向并首先处理最小的部分,替换它们,这样就不会中断较大的部分,然后再处理较大的部分?
Ok, so I tried searching around first but I didn't exactly know how to word this question or a search phrase. Let me explain.
I have data that looks like this:
<!-- data:start -->
<!-- 0:start -->
<!-- 0:start -->0,9<!-- 0:stop -->
<!-- 1:start -->0,0<!-- 1:stop -->
<!-- 2:start -->9,0<!-- 2:stop -->
<!-- 3:start -->9,9<!-- 3:stop -->
<!-- 4:start -->0,9<!-- 4:stop -->
<!-- 0:stop -->
<!-- 1:start -->
<!-- 0:start -->1,5<!-- 0:stop -->
<!-- 1:start -->1,6<!-- 1:stop -->
<!-- 2:start -->3,6<!-- 2:stop -->
<!-- 3:start -->3,8<!-- 3:stop -->
<!-- 4:start -->4,8<!-- 4:stop -->
<!-- 1:stop -->
<!-- 2:start -->
<!-- 0:start -->0,7<!-- 0:stop -->
<!-- 1:start -->1,7<!-- 1:stop -->
<!-- 2:stop -->
<!-- data:stop -->
So it's basically a bunch of points. Here is the code I'm currently using to try and parse it so that it would create an array like so:
Array (
0 => Array (
0 => "0,9",
1 => "0,0",
2 => "9,0",
3 => "9,9",
4 => "0,9"
),
1 => Array (
0 => "1,5",
1 => "1,6",
2 => "3,6",
3 => "3,8",
4 => "4,8"
),
2 => Array (
0 => "0,7",
1 => "1,7"
)
)
However, it is returning an array that looks like this:
Array (
0 => "0,9",
1 => "0,0",
2 => "9,0"
)
Viewing the larger array that I have on my screen, you see that it's setting the first instance of that variable when matching. So how do I get it to find the widest match first and then process the insides. Here is the function I am currently using:
function explosion($text) {
$number = preg_match_all("/(<!-- ([\w]+):start -->)\n?(.*?)\n?(<!-- \\2:stop -->)/s", $text, $matches, PREG_SET_ORDER);
if ($number == 0) return $text;
else unset($item);
foreach ($matches as $item) if (empty($data[$item[2]])) $data[$item[2]] = $this->explosion($item[3]);
return $data;
}
I'm sure it will be something stupid and simple that I've overlooked, but that just makes it an easy answer for you I suppose.
EDIT: Here is a full output log of the entire data set that I took this sample from. The tags are printed out (replace them with > and <) and it's all inside a giant <code></code>
element for easy reading.
Here is the part that's messing up:
Array ( [0] => <!-- 0:start --> <!-- 0:start -->0,9<!-- 0:stop --> [1] => 0 [2] => <!-- 0:start -->0,9 )
0 => <!-- 0:start -->0,9
So it's stopping at the first occurrence of the stop tag for another piece inside of it. Should I be thinking the opposite direction and processing the smallest pieces first, replacing those so it won't interrupt the larger pieces, and then processing the larger pieces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
干得好:
Here you go:
嗯,这对我有用:
您的代码的问题是它正在获取子标签和标记值。当然,在浏览器中打印时它会被隐藏,因此请尝试记录它以进行调试。
Well this works for me:
The problem with your code was that it was picking up the subtags and the tagged values. Of course it would be hidden when printed in browser, so try taking a log of it for debugging purpose.