为什么此正则表达式仅处理模式的最后一次出现

我正在尝试创建一个正则表达式,它将使用标记代码创建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>
tldtc007 回答:为什么此正则表达式仅处理模式的最后一次出现

好吧,睡前我有几分钟的时间可以花在手机上,所以我听了Wiktor的评论,然后整理了一系列preg_函数,试图将bbcode转换为html。我没有bbcode的经验,所以我只是在解决您的样本输入问题,而不考虑附带情况。我认为php在某处有一个bbcode解析器库,但是我不知道您的bbcode语法是否是标准语法。

一些分解的实施模式。

首先,隔离文档中的每个完整[table]...[/table]字符串。 (Regex101 Demo~\[table]\R*([^[]*(?:\[(?!/?table])[^[]*)*)\R*\[/table]~将匹配字符串,并将完全匹配项传递为$m[0],并将表标记之间的子字符串传递为$m[1]BBTableToHTML()

接下来,BBTableToHTML()将在$m[1]字符串上进行3次单独传递。每个模式都将其各自匹配的字符串发送到关联的自定义函数,并返回修改后的字符串。

在将更新后的$m[1]BBTableToHTML()发送回echo之前,您所需的<table...></table>标签将被$m[1]保留。 / p>

preg_replace_callback_array()模式的演示:

  1. ~\[\*\*]([^[]*(?:\[(?!/?\*\*])[^[]*)*)\[/\*\*]~ https://regex101.com/r/thINHQ/2
  2. ~(?:\[\*].*\R*)+~ https://regex101.com/r/thINHQ/3
  3. ~\[\*](.*)~ https://regex101.com/r/thINHQ/4

代码:(Demo

$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
本文链接:https://www.f2er.com/3074305.html

大家都在问