我对 preg_match_all 做错了什么?
目标是找到诸如 some content here
之类的集合,并处理每个集合以将其放入数组,但每次它都找不到匹配项并回显我的 die 语句而不打印任何匹配项。 注意:开始标签和结束标签之间的内容通常跨越多行。
这是当前正在执行的函数。
function boom($data) {
$number = preg_match_all("/(<!-- ([\w]+):start -->)(.*?)(<!-- \\2:stop -->)/", $data, $matches, PREG_SET_ORDER);
if ($number == 0) $data['content'] = $data;
//else unset($data);
foreach ($matches as $item) print_r($item)."\n\n"; // $data[$item[2]] = explode("<!-- breaker -->", $item[3]);
die("<code>".str_replace("\n", "<br />", htmlentities($data))."</code>");
return $data;
}
The object is to find sets such as <!-- content:start -->some content here<!-- content:stop -->
and process each one to put it into an array, but every time it find no matches and echos my die statement without printing any matches out. Note: The content in between the start and end tags usually spans multiple lines.
This is the current function that is being executed.
function boom($data) {
$number = preg_match_all("/(<!-- ([\w]+):start -->)(.*?)(<!-- \\2:stop -->)/", $data, $matches, PREG_SET_ORDER);
if ($number == 0) $data['content'] = $data;
//else unset($data);
foreach ($matches as $item) print_r($item)."\n\n"; // $data[$item[2]] = explode("<!-- breaker -->", $item[3]);
die("<code>".str_replace("\n", "<br />", htmlentities($data))."</code>");
return $data;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的文本有多行,只需将
/s
修饰符添加到您的模式中(以使.
匹配换行符)。If your text goes on multiple lines, just add the
/s
modifier to your pattern (to make.
match newlines).