使用 preg_match_all 更改所有表标签

问题描述 投票:0回答:1

我想将字符串中的所有标签更改为[table1],[table2]等集合。

例如,

Hello there <table class="table1"><tr><td></td></tr></table>

Some text here

<table class="table2"><tr><td></td></tr></table>

Some text here

<table class="table3"><tr><td></td></tr></table>

致:

Hello there [table1]

Some text here

[table2]

Some text here

[table3]

使用

preg_match_all("@\<table (\s\S+)@s", $table_in_string, $match);
foreach ($match[1] as $key => $k) {
                                
}

我这里的正则表达式好像不起作用。

php regex preg-match preg-match-all
1个回答
0
投票

pregl_replace示例:

$text = preg_replace( '/<table.*?class="(table\d+)".*?<\/table>/s', '[$1]', $text );
© www.soinside.com 2019 - 2024. All rights reserved.