php - 为什么这个正则表达式只匹配模式的最后一次出现
问题描述
我正在尝试创建一个正则表达式,它将用标记代码创建 html。
当尝试替换 [table] 标记的一部分时,它只替换最后一次出现。
我有以下正则表达式(PHP):
/(\[table].*)\[\|](.*\[\/table])/s
替换模式:
$1</td><td>$2
以及以下测试字符串:
[table]<thead>
<th>head1</th><th>head2</th></thead>
[*]test1[|]test2
[*]test1[|]test2
[/table]
它应该产生以下内容:
[table]<thead>
<th>head1</th><th>head2</th></thead>
[*]test1</td><td>test2
[*]test1</td><td>test2
[/table]
但它实际上是这样的:
[table]<thead>
<th>head1</th><th>head2</th></thead>
[*]test1[|]test2
[*]test1</td><td>test2
[/table]
问题在于,它[|]
在其他标记代码中使用但不应替换为</td><td>
澄清一下:我有一个表“bb-code”
[table]
[**]header1[||]header2[||]header3[||]...[/**]
[*]child1.1[|]child1.2[|]child1.3[|]...
[*]child2.1[|]child2.2[|]child2.3[|]...
[*]child3.1[|]child3.2[|]child3.3[|]...
[*]...[|]...[|]...[|]...
[/table]
我希望它变成这样:
<table class="ui compact stripet yellow table">
<thead>
<tr>
<th>header1</th>
<th>header2</th>
<th>header3</th>
<th>....</th>
</tr>
</thead>
<tbody>
<tr>
<td>child1.1</td>
<td>child1.2</td>
<td>child1.3</td>
<td>...</td>
</tr>
<tr>
<td>child2.1</td>
<td>child2.2</td>
<td>child2.3</td>
<td>...</td>
</tr>
<tr>
<td>child3.1</td>
<td>child3.2</td>
<td>child3.3</td>
<td>...</td>
</tr>
</tbody>
</table>
解决方案
好的,我在睡前有几分钟的时间在手机上,所以我跑了 Wiktor 的评论并编写了一系列preg_
函数来尝试将你的 bbcode 转换为 html。我对 bbcode 没有任何经验,所以我纯粹是针对您的示例输入而不考虑边缘情况。我认为 php 在某处有一个 bbcode 解析器库,但我不知道你的 bbcode 语法是否是标准的。
实现的模式的一些分解。
首先,隔离[table]...[/table]
文档中的每个完整字符串。( Regex101 Demo )~\[table]\R*([^[]*(?:\[(?!/?table])[^[]*)*)\R*\[/table]~
将匹配字符串并传递完整匹配 as$m[0]
和表标签之间的子字符串 as $m[1]
to BBTableToHTML()
。
接下来,BBTableToHTML()
将对$m[1]
字符串进行 3 次单独的传递。这些模式中的每一个都会将它们各自匹配的字符串发送到关联的自定义函数并返回修改后的字符串。
在将更新$m[1]
后BBTableToHTML()
的内容发送到之前echo
,您想要的<table...>
和</table>
标签将结束$m[1]
。
模式演示preg_replace_callback_array()
:
~\[\*\*]([^[]*(?:\[(?!/?\*\*])[^[]*)*)\[/\*\*]~
https://regex101.com/r/thINHQ/2~(?:\[\*].*\R*)+~
https://regex101.com/r/thINHQ/3~\[\*](.*)~
https://regex101.com/r/thINHQ/4
代码:(演示)
$bbcode = <<<BBCODE
[b]Check out this demo[/b]
¯\_(ツ)_/¯
[table]
[**]header1[||]header2[||]header3[||]...[/**]
[*]child1.1[|]child1.2[|]child1.3[|]...
[*]child2.1[|]child2.2[|]child2.3[|]...
[*]child3.1[|]child3.2[|]child3.3[|]...
[*]...[|]...[|]...[|]...
[/table]
simple text
[table]
[**]a 1[||]and a 2[/**]
[*]A[|]B
[*]C[|]D
[/table]
[s]3, you're out[/s]
blah
BBCODE;
function BBTableToHTML($m) {
return "<table class=\"ui compact stripet yellow table\">\n" .
preg_replace_callback_array(
[
'~\[\*\*]([^[]*(?:\[(?!/?\*\*])[^[]*)*)\[/\*\*]~' => 'BBTHeadToHTML',
'~(?:\[\*].*\R*)+~' => 'BBTBodyToHTML',
'~\[\*](.*)~' => 'BBTBodyRowToHTML'
],
$m[1]
) .
"</table>";
}
function BBTHeadToHTML($m) {
return "\t<thead>\n" .
"\t\t<tr>\n\t\t\t<th>" . str_replace('[||]', "</th>\n\t\t\t<th>", $m[1]) . "</th>\n\t\t</tr>\n" .
"\t</thead>";
}
function BBTBodyToHTML($m) {
return "\t<tbody>\n{$m[0]}\t</tbody>\n";
}
function BBTBodyRowToHTML($m) {
return "\t\t<tr>\n\t\t\t<td>" . str_replace('[|]', "</td>\n\t\t\t<td>", $m[1]) . "</td>\n\t\t</tr>";
}
echo preg_replace_callback(
'~\[table]\R*([^[]*(?:\[(?!/?table])[^[]*)*)\R*\[/table]~',
'BBTableToHTML',
$bbcode
);
输出:
[b]Check out this demo[/b]
¯\_(ツ)_/¯
<table class="ui compact stripet yellow table">
<thead>
<tr>
<th>header1</th>
<th>header2</th>
<th>header3</th>
<th>...</th>
</tr>
</thead>
<tbody>
<tr>
<td>child1.1</td>
<td>child1.2</td>
<td>child1.3</td>
<td>...</td>
</tr>
<tr>
<td>child2.1</td>
<td>child2.2</td>
<td>child2.3</td>
<td>...</td>
</tr>
<tr>
<td>child3.1</td>
<td>child3.2</td>
<td>child3.3</td>
<td>...</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
</tbody>
</table>
simple text
<table class="ui compact stripet yellow table">
<thead>
<tr>
<th>a 1</th>
<th>and a 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td>B</td>
</tr>
<tr>
<td>C</td>
<td>D</td>
</tr>
</tbody>
</table>
[s]3, you're out[/s]
blah
推荐阅读
- javascript - 基于变量获取 (0,3) (3,6) (6,9) 的逻辑
- java - 如何从 SubClass 实例中获取定义类实例?
- git - 如何使用 Visual Studio Code 为现有项目使用 Github 个人访问令牌
- python - python 从 azure 存储帐户读取
- javascript - 关闭 IonModel 和 IonPicker 后页面滚动回顶部
- controller - Yii2 控制器触发两次/取决于浏览器
- python - 如果 Xpath 不存在,播放 mp3 - Python
- azure - 触发器主体属性的全局参数和在 @triggerbody().folderpath 上使用拆分功能
- google-cloud-platform - Google OAuth 同意屏幕 - 范围错误
- unicode - Deno:从 Windows 终端提示符阅读时如何处理口音?