【问题标题】:Replace all the <a> tags in string except with one specific ID using preg_replace使用 preg_replace 替换字符串中的所有 <a> 标记,除了一个特定的 ID
【发布时间】:2017-04-13 06:30:30
【问题描述】:

我有这个 HTML 代码:

$html='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="font-family: Helvetica Neue, Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
    <head>
        <meta name="viewport" content="width=device-width" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    </head>

    <body itemscope itemtype="http://schema.org/EmailMessage" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%; line-height: 1.6em; background-color: #f6f6f6; margin: 0;" bgcolor="#f6f6f6">

        <table class="body-wrap" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; background-color: #f6f6f6; margin: 0;" bgcolor="#f6f6f6"><tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
                <td class="container" width="600" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto;" valign="top">
                    <div class="content" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; max-width: 600px; display: block; margin: 0 auto; padding: 20px;">
                        <table class="main" width="100%" cellpadding="0" cellspacing="0" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; border-radius: 3px; background-color: #fff; margin: 0; border: 1px solid #e9e9e9;" bgcolor="#fff"><tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                <td class="alert alert-warning" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 16px; vertical-align: top; color: #fff; font-weight: 500; text-align: center; border-radius: 3px 3px 0 0; background-color: #1ab394; margin: 0; padding: 20px;" align="center" bgcolor="#1ab394" valign="top">
                                    Following change has occurred in the 1st fault 4 notification.
                                </td>
                            </tr><tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-wrap" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 20px;" valign="top">
                                    <table width="100%" cellpadding="0" cellspacing="0" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td id="notification-detail" class="content-block" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                                <a class="btn-link removable no-padding" href="javascript:void(0);" onclick="goToListingByType()">Fault</a>, <a class="btn-link no-padding" href="javascript:void(0);" 
    onclick="goToDetailPageByType()">1st fault 4 notification, 14077</a> Naveed Saleem has updated the Due date of this Activity to 23rd November, 2016
                                            </td>
                                        </tr>
                                        <tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                                <a href="{{URL}}" id="login-link" class="btn-primary" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #FFF; text-decoration: none; line-height: 2em; font-weight: bold; text-align: center; cursor: pointer; display: inline-block; border-radius: 5px; text-transform: capitalize; background-color: #1ab394; margin: 0; border-color: #1ab394; border-style: solid; border-width: 10px 20px;">Login Whuntu</a>
                                            </td>
                                        </tr><tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                                Thanks for choosing Company Inc.
                                            </td>
                                        </tr></table></td>
                            </tr></table><div class="footer" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; clear: both; color: #999; margin: 0; padding: 20px;">
                            <table width="100%" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                <tr style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                    <td class="aligncenter content-block" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; vertical-align: top; color: #999; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top">Copyright © 2014-2016, Company Name, United Kingdom. All rights reserved.</td>
                                </tr></table></div></div>
                </td>
                <td style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
            </tr></table></body>
</html>
';

我想替换所有&lt;a&gt; 标记,但那些具有特定属性 ID 的标记除外。

我是正则表达式的菜鸟。任何人都可以请帮忙。我发现这个正则表达式替换了基于类的所有标签:

$html = preg_replace('/<(a).*?class="\s*(?:.*\s)?removable(?:\s[^"]+)?\s*"[^\>]*>(.*)<\/\1>/i', '\2', $html);

但我确实想要我所说的,即用 span 替换所有 &lt;a&gt; 标签,除了那些具有特定 ID 的标签。

非常感谢您的帮助

【问题讨论】:

  • 如果你在谷歌上搜索“正则表达式条件替换”,你会遇到这样的语法:(?(?=regex)then|else)。这应该可以帮助您朝着正确的方向前进。
  • 这不是正则表达式的工作,请使用带有 DOMXPath 的 DOMDocument。

标签: php regex preg-replace regular-language


【解决方案1】:

当你要解析结构化语言时,使用这种结构,不要像解析简单的文本那样解析它。 PHP 提供内置工具来解析 HTML 和 XML:

  • DOMDocument 类用于构建和编辑 DOM 树
  • DOMXPath 类查询 DOM 树

例子:

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();

$xp = new DOMXPath($dom);

// select all the link nodes that don't have an id attribute
// to discard a specific id, you can change the predicate to: not(@id="yourid")
$linkNodeList = $xp->query('//a[not(@id)]');

foreach ($linkNodeList as $linkNode) {
    // create a new span element where to store all children nodes of the link node
    $spanNode = $dom->createElement('span');
    // append each child node to the span node
    foreach($linkNode->childNodes as $childNode) {
        $spanNode->appendChild($childNode->cloneNode(true));
    }
    // replace the link node with the span node
    $linkNode->parentNode->replaceChild($spanNode, $linkNode);
}

echo $dom->saveHTML();

【讨论】:

    猜你喜欢
    • 2014-06-03
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 2014-03-08
    • 2016-05-31
    • 1970-01-01
    • 2021-04-28
    • 2011-12-17
    相关资源
    最近更新 更多