-2

我有内容:

<table cellpadding="0" cellspacing="0" class="tbl-photo right">
    <tbody>
        <tr>
            <td>
                <amp-img layout="responsive" height="500" width="760" alt="" src="https://example.com/uploads/images/0 Богдан-Цырдя(1).jpg" class="i-amphtml-element i-amphtml-layout-responsive i-amphtml-layout-size-defined i-amphtml-layout">
                    <i-amphtml-sizer style="padding-top: 65.7895%;"></i-amphtml-sizer>
                    <img decoding="async" alt="" src="https://example.com/uploads/images/0 Богдан-Цырдя(1).jpg" class="i-amphtml-fill-content i-amphtml-replaced-content">
                </amp-img>
            </td>
        </tr>
        <tr>
            <td class="date"> Foto: example.com</td>
        </tr>
    </tbody>
</table>

我需要删除这张桌子preg_replace

我只需要删除这个带有 class 的表tbl-photo right。我怎么能做到这一点?

我的表达:

$pattern = '~<table(?>(?!</?table).)*</table>~is';
$output = preg_replace($pattern, '', $body);

但这会删除所有表格...

4

2 回答 2

0

我建议您使用http://buildregex.com/。这是一个构建正则表达式的非常棒且简单的页面!你肯定会发现它很有用。

如果您不太喜欢正则表达式,那么其中一个应该可以完成这项工作:

/(?:\<table)(?:.*)(?:tbl\-photo\ right)(?:.*)(?:\/table\>)/

/(?:\<table)(?:[^table\>]*)(?:tbl\-photo\ right)(?:[^table\>]*)(?:\/table\>)/

于 2018-09-25T07:54:29.667 回答
0

试试正则表达式<table(?=[\s\S]*(class="tbl-photo right"))[\s\S]*?<\/table>

这将匹配表标签与类名之间的所有内容tbl-photo right

正则表达式

于 2018-09-25T08:47:05.850 回答